• Stars
    star
    217
  • Rank 182,446 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High performance immutable lib alternative to immer with the same api, based on shallow copy on read and mark modified on write mechanism.

limu 🍋

limu is short for love 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 than 2 or 20 times faster than immer in different situations. Click this online 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 immer in different situations.
  • Natural Support for map and Set.

Pay attention, limu can only run on JavaScript runtime that supports proxy

Performance ⚡️

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 this immer case )

test 2 test 2

The performance testing process is as follows

git clone https://github.com/tnfe/limu
cd limu
npm i
cd benchmark
npm i
node opBigData.js // trigger test execution, the console echoes the result
# or
node caseOnlyRead.js
npm run s1
npm run s2
npm run s3
npm run s4

You are very welcome to submit your test to the benchmark directory or test directory

Quick Start

install

npm i limu

apis

import { produce, createDraft, finishDraft } from 'limu';

produce

const baseState = {
  a: 1,
  b: [1, 2, 3],
  c: {
    c1: { n: 1 },
    c2: { m: 2 },
  },
};
const nextState = produce(baseState, (draft) => {
  draft.a = 2;
  draft.b['2'] = 100;
});

console.log(nextState === baseState); // false
console.log(nextState.a === baseState.a); // false
console.log(nextState.b === baseState.b); // false
console.log(nextState.c === baseState.c); // true

Currying call

const producer = produce((draft) => {
  draft.a = 2;
  draft.b['2'] = 100;
});
const nextState = producer(baseState);

createDraft, finishDraft

const draft = createDraft(baseState);
draft.a = 2;
draft.b = [];
const nextState = finishDraft(draft);

console.log(nextState === baseState); // false
console.log(nextState.a === baseState.a); // false
console.log(nextState.b === baseState.b); // false
console.log(nextState.c === baseState.c); // true

Experience limu on the console

logo

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, open limu doc site and right-click to open the console.

  • 2, visit unpkg, right-click to open the console, and then paste the following code to load js

function loadJs(url) {
  const dom = document.createElement('script');
  dom.src = url;
  document.body.appendChild(dom);
}

loadJs('https://unpkg.com/[email protected]/dist/limu.min.js'); // load limu umd bundle
loadJs('https://unpkg.com/[email protected]/dist/immer.umd.production.min.js'); // load immer umd bundle

Then you can paste below codes to run

  • case 1
function oneCase(produce) {
  const demo = { info: Array.from(Array(10000).keys()) };
  produce(demo, (draft) => {
    draft.info[2000] = 0;
  });
}

function runBenchmark(produce, label) {
  const start = Date.now();
  const limit = 100;
  for (let i = 0; i < limit; i++) {
    oneCase(produce);
  }
  console.log(`${label} avg spend ${(Date.now() - start) / limit} ms`);
}

function run() {
  immer.setAutoFreeze(false);
  runBenchmark(immer.produce, 'immer,');
  runBenchmark(limu.produce, 'limu,');
}
  • case 2
lib = window.limu; // or lib = window.immer
const base = {
  a: 1,
  b: { b1: 1, b2: 2, b3: { b31: 1 } },
  c: [1, 2, 3],
  d: { d1: 1000 },
};
const draft = lib.createDraft(base);
draft.a = 200;
draft.b.b1 = 100;
console.log(draft);
draft.c.push(4);
const final = lib.finishDraft(draft);
console.log(base === final); // false
console.log(base.a === final.a); // false
console.log(base.b === final.b); // false
console.log(base.b.b3 === final.b.b3); // true
console.log(base.c === final.c); // false
console.log(base.d === final.d); //true

Higher observability will greatly improve the development and debugging experience, as shown in the figure below, after unfolding the limu draft, you can observe all data nodes of the draft in real time

image

And the immer or mutative expansion is like this image

License

Copyright (c) Tencent Corporation. All rights reserved.

Limu is released under the MIT License(https://opensource.org/licenses/MIT)

More Repositories

1

TNT-Weekly

🙈 🙉 🙊 每周为您推荐国内外前端领域最新的优秀文章以及行业进展
5,158
star
2

FFCreator

A fast video processing library based on node.js (一个基于node.js的高速视频制作库)
JavaScript
2,816
star
3

awesome-blackmagic

🎭 ♠♥奇技淫巧 💠黑魔法大集合♦♣ 👺
1,027
star
4

wp2vite

一个让webpack项目支持vite的前端项目的转换工具。A front-end project automatic conversion tool。
JavaScript
709
star
5

shida

《视搭》是一个视频可视化搭建项目。您可以通过简单的拖拽方式快速生产一个短视频,使用方式就像易企秀或Maka 等 h5 搭建工具一样的简单,仅抛砖引玉希望您喜欢。
Vue
569
star
6

FEDiagram

图说前端>>收集各种前端技术图谱 🚕🚖🚗🚚🚛🚜
440
star
7

csijs

CSI.JS是一个特别的前端日志系统,帮你快速重建犯罪现场。
JavaScript
275
star
8

bbo

bbo is a utility library of zero dependencies for javascript. 🍖🌭🍔
JavaScript
275
star
9

vue3-infinite-list

一个支持百万数量级的Vue3无限滚动列表组件
TypeScript
224
star
10

awesome-state

collection of state management lib
187
star
11

transx

一个小巧玲珑的 vue 组件切换动画库, 支持 20 几种动画切换方式
JavaScript
183
star
12

clean-state

🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
TypeScript
120
star
13

tntweb-admin

react admin management system template
CSS
38
star
14

awesome-ffcreator

awesome ffcreator projects
30
star
15

jscalpel

A small feature library that makes it easier to manipulate objects
JavaScript
27
star
16

manage-table

对antd的table进行扩展,支持配置展示列
JavaScript
13
star
17

data-filler

make your backend response data shape reliable, with data-filler you can stay away from optional chain
JavaScript
2
star
18

html3canvas

html convert to canvas
TypeScript
1
star
19

hel

a solution of using micro module for frontend development
TypeScript
1
star