A simple bridge to ClojureScript's persistent data structures and supporting APIs for vanilla JavaScript. Pull requests welcome.
The API now uses idiomatic JavaScript naming conventions.
Mori is considerably faster across the board thanks to recent enhancements to the ClojureScript compiler. For users who would like to benchmark their immutable data structure implementations against Mori, Mori now exposes direct arity invokes which eliminates previous calling overheads from arity dispatching. See Benchmarking for more information.
Mori hash maps now default to ClojureScript ArrayMaps that are
automatically promoted to PersistentHashMaps as needed. ArrayMaps
deliver considerably better performance at small sizes and when simple
keys are at play. For example a Mori hash map with less than or equal
to eight keys can now be built nearly an order of magnitude faster than
Immutable.js 3.6.2 Maps.
All Mori collections support ES6 iteration via foo[Symbol.iterator]
or foo["@@iterator"].
- A functional API, data structures do not have public methods
- Faster, ClojureScript data structures have been subjected to more real world usage and continuous benchmarking for nearly 4 years
- Larger, gzipped the base Mori module is about 6K larger than Immutable.js
You can install the latest release via npm:
npm install moriThe installed package contains a single optimized JavaScript file mori.js.
Load mori in your Node.js programs as you would any other module:
varmori=require("mori");In a browser, you can load mori with a script tag, as you would any other JavaScript library:
<scriptsrc="mori.js" type="text/javascript"></script>You can also load it as an AMD module, e.g. with RequireJS.
You can use it from your projects like so:
varinc=function(n){returnn+1;};mori.intoArray(mori.map(inc,mori.vector(1,2,3,4,5)));// => [2,3,4,5,6]Efficient non-destructive updates!
varv1=mori.vector(1,2,3);varv2=mori.conj(v1,4);v1.toString();// => '[1 2 3]'v2.toString();// => '[1 2 3 4]'varsum=function(a,b){returna+b;};mori.reduce(sum,mori.vector(1,2,3,4));// => 10Lazy sequences!
var_=mori;_.intoArray(_.interpose("foo",_.vector(1,2,3,4)));// => [1, "foo", 2, "foo", 3, "foo", 4]Or if it's more your speed, use it from CoffeeScript!
inc= (x) -> x+1r=mori.map inc, mori.vector(1,2,3,4,5)
mori.intoArray rYou can find extensive documentation and examples here.
For vectors and maps we provide an efficient thaw and freeze operations:
varm=mori;// ~220ms with V8 version 3.29.80 MBP 2.26ghzfor(varj=0;j<10;j++){vars=newDate();vararr=[];for(vari=0;i<10000000;i++){arr.push(i);}print("Array push "+arr.length+" items "+((newDate())-s));gc();}// ~70msfor(varj=0;j<10;j++){s=newDate();varmv=m._thaw(m.vector());for(vari=0;i<10000000;i++){mv=m._conj.f2(mv,i);}varv=m._freeze(mv);print("Mutable vector conj "+m.count(v)+" items "+((newDate())-s));gc();}All Mori maps and sets support all the non-mutating methods of the
proposed ES6
Map
and
Set
interfaces. The main difference with the spec is that key lookup is
based on value not reference. keys, values, and entries methods
return the proposed mutable iterators:
varm=mori;varh=m.hashMap("foo",1,"bar",2);h.has("foo");// => trueh.get("foo");// => 1variter=h.keys();iter.next();// => {done: false, value: "foo"}This feature is subject to changes in the ES6 proposal.
Mori includes Transducers. Zero allocation collection operations FTW:
varm=mori;vara=[];for(vari=0;i<1000000;i++){a.push(i);}// make it immutablevarv=m.into(m.vector(),a);functiontime(f){vars=newDate();f();console.log(((newDate())-s)+"ms");}// ~190ms V8 version 3.29.80 MBP 2.26ghztime(function(){varxf=m.comp(m.map(m.inc),m.map(m.inc),m.map(m.inc));returnm.transduce(xf,m.completing(m.sum),0,v);},10);// ~440mstime(function(){returna.map(m.inc).map(m.inc).map(m.inc).reduce(function(a,b){returna+b;},0);},10);You will first need to install the Java SDK, if it's not already installed on your system.
On Windows, you will need to manually install
Leiningen. On UNIX-like
systems, Leiningen will be installed within the project automatically
if the lein executable is not found on your path or if your lein
version predates 2.0.0.
git clone https://github.com/swannodette/mori.git
cd mori./scripts/build.shnpm run-script build./scripts/build.ps1The build process will generate an optimized JavaScript file
mori.js, which is suitable for use with Node.js, or in a Web browser
or other JavaScript environments. You can also load it as an AMD
module.
Copyright (C) 2012-2015 David Nolen and contributors
Distributed under the Eclipse Public License, the same as Clojure.
