- Notifications
You must be signed in to change notification settings - Fork14
High performance immutable lib alternative to immer with the same api, based on shallow copy on read and mark modified on write mechanism.
License
tnfe/limu
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
limu is short forlove immutable, born for efficient creation and operation of immutable object, based on shallow copy on read and mark modified on write mechanism.
It is fast, It is nearly more than2 or 20 times faster thanimmer in different situations. Click thisonline perf demo to review the amazing result.
No freeze by default, limu is 10 times or more faster than Immer in most scenarios
- Debugging friendly, view draft directly anytime without
current. - Smaller package, only 4.3kb gzip.
- No freeze by default, faster than
immerin different situations. - Natural Support for map and Set.
Pay attention, limu can only run on JavaScript runtime that supports proxy
No freeze by default, limu is 10 times or more faster than Immer in most scenarios after 3.7 version, limu is now the fastest immutable js lib of all ( faster than immer and mutative ).
test 1 (inspired by thisimmer case )
The performance testing process is as follows
git clone https://github.com/tnfe/limucd limunpm icd benchmarknpm inode opBigData.js // triggertest execution, the console echoes the result# ornode caseOnlyRead.jsnpm run s1npm run s2npm run s3npm run s4
You are very welcome to submit your test to thebenchmark directory ortest directory
install
npm i limu
apis
import{produce,createDraft,finishDraft}from'limu';
constbaseState={a:1,b:[1,2,3],c:{c1:{n:1},c2:{m:2},},};constnextState=produce(baseState,(draft)=>{draft.a=2;draft.b['2']=100;});console.log(nextState===baseState);// falseconsole.log(nextState.a===baseState.a);// falseconsole.log(nextState.b===baseState.b);// falseconsole.log(nextState.c===baseState.c);// true
Currying call
constproducer=produce((draft)=>{draft.a=2;draft.b['2']=100;});constnextState=producer(baseState);
constdraft=createDraft(baseState);draft.a=2;draft.b=[];constnextState=finishDraft(draft);console.log(nextState===baseState);// falseconsole.log(nextState.a===baseState.a);// falseconsole.log(nextState.b===baseState.b);// falseconsole.log(nextState.c===baseState.c);// true
As limu is an immutable js library based on shallow copy on read and mark modified on write. Based on this mechanism, so it is more friendly to debugging. You can copy the following code to the console experience
there are 2 ways to quickly experience limu and compare limu with immer.
1, openlimu doc site and right-click to open the console.
2, visitunpkg, right-click to open the console, and then paste the following code to load js
functionloadJs(url){constdom=document.createElement('script');dom.src=url;document.body.appendChild(dom);}loadJs('https://unpkg.com/limu@3.5.5/dist/limu.min.js');// load limu umd bundleloadJs('https://unpkg.com/immer@9.0.21/dist/immer.umd.production.min.js');// load immer umd bundle
Then you can paste below codes to run
- case 1
functiononeCase(produce){constdemo={info:Array.from(Array(10000).keys())};produce(demo,(draft)=>{draft.info[2000]=0;});}functionrunBenchmark(produce,label){conststart=Date.now();constlimit=100;for(leti=0;i<limit;i++){oneCase(produce);}console.log(`${label} avg spend${(Date.now()-start)/limit} ms`);}functionrun(){immer.setAutoFreeze(false);runBenchmark(immer.produce,'immer,');runBenchmark(limu.produce,'limu,');}
- case 2
lib=window.limu;// or lib = window.immerconstbase={a:1,b:{b1:1,b2:2,b3:{b31:1}},c:[1,2,3],d:{d1:1000},};constdraft=lib.createDraft(base);draft.a=200;draft.b.b1=100;console.log(draft);draft.c.push(4);constfinal=lib.finishDraft(draft);console.log(base===final);// falseconsole.log(base.a===final.a);// falseconsole.log(base.b===final.b);// falseconsole.log(base.b.b3===final.b.b3);// trueconsole.log(base.c===final.c);// falseconsole.log(base.d===final.d);//true
Higher observability will greatly improve the development and debugging experience, as shown in the figure below, after unfolding thelimu draft, you can observe all data nodes of the draft in real time

And the immer or mutative expansion is like this
Copyright (c) Tencent Corporation. All rights reserved.
Limu is released under the MIT License(https://opensource.org/licenses/MIT)
About
High performance immutable lib alternative to immer with the same api, based on shallow copy on read and mark modified on write mechanism.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.


