unadlib / mutative

Efficient immutable updates, 2-6x faster than naive handcrafted reducer, and more than 10x faster than Immer.
http://mutative.js.org/
MIT License
1.53k stars 16 forks source link

Plan: production-ready version coming soon #8

Closed unadlib closed 6 months ago

unadlib commented 1 year ago

Mutative has fixed some edge cases, as well as support for reducers, and full support for the JSON Patch spec. Mutative will not consider supporting circular references and IE browser.

Do you have any comments or suggestions about Mutative official v1?

victordidenko commented 1 year ago

Is immer performance issue still a thing? Because link performance issue from readme opens 0 (zero) issues.

unadlib commented 1 year ago

Is immer performance issue still a thing? Because link performance issue from readme opens 0 (zero) issues.

Of course, Its performance issues are not solved. In the latest Immer v10 and mutative benchmark performance tests, Immer v10 does not have significant performance improvements.

fantasticsoul commented 11 months ago

💐恭喜mutative,通过了limu的内置测试,测试流程

git clone https://github.com/tnfe/limu
cd limu
npm i
cd benchmark
npm i
cd ../

然后修改test/_util.ts 里头部注释

import * as limu from '../src/index'; // test limu source code
// import { lib as limu } from '../benchmark/lib/mutative'; // test mutative

改为

// import * as limu from '../src/index'; // test limu source code
import { lib as limu } from '../benchmark/lib/mutative'; // test mutative

修改jest.config.js里测试覆盖文件,解开<rootDir>/benchmark/lib/mutative.js,关闭<rootDir>/src/**/*.ts

  collectCoverageFrom: [
    '<rootDir>/src/**/*.ts',
    // '<rootDir>/benchmark/lib/mutative.js',
    '!<rootDir>/src/inner-types.ts',
    '!<rootDir>/src/core/helper.ts',
  ],

接着执行 npm run test 即可,mutative通过了 314 个测试用例,的确可以生产可用了(当然了limu也通过了这些测试用例,即将投入正式使用,并成为 helux v3的重要驱动引擎 ),下图为mutative的测试用例结果 image

关于性能方面,mutative在大部分场景的都表现更佳👍🏻,性能测试流程如下

cd benchmark
node caseOnlyRead.js // 触发测试执行,控制台回显结果

可通过注入ST值调整不同的测试策略,例如 ST=1 node caseOnlyRead.js,不注入时默认为 4

准备了2个用例,只读场景caseOnlyRead,和读写场景caseReadWrite

测试结果如下

只读

1 ST=1 node caseOnlyRead.js

loop: 200, immer avg spend 14.6 ms
loop: 200, limu avg spend 12.68 ms
loop: 200, mutative avg spend 9.215 ms
loop: 200, pstr avg spend 4 ms
loop: 200, native avg spend 0.37 ms

2 ST=2 node caseOnlyRead.js

loop: 200, immer avg spend 16.63 ms
loop: 200, limu avg spend 15.02 ms
loop: 200, mutative avg spend 11.685 ms
loop: 200, pstr avg spend 5.89 ms
loop: 200, native avg spend 1.345 ms

3 ST=3 node caseOnlyRead.js

loop: 200, immer avg spend 13.525 ms
loop: 200, limu avg spend 11.54 ms
loop: 200, mutative avg spend 9.53 ms
loop: 200, pstr avg spend 3.79 ms
loop: 200, native avg spend 0.505 ms

4 ST=4 node caseOnlyRead.js

loop: 200, immer avg spend 12.965 ms
loop: 200, limu avg spend 11.2 ms
loop: 200, mutative avg spend 8.98 ms
loop: 200, pstr avg spend 4.045 ms
loop: 200, native avg spend 1.065 ms

读写

1 ST=1 node caseReadWrite.js

loop: 200, immer avg spend 4.045 ms
loop: 200, limu avg spend 4.47 ms
loop: 200, mutative avg spend 2.11 ms
loop: 200, pstr avg spend 8.835 ms
loop: 200, native avg spend 0.225 ms

2 ST=2 node caseReadWrite.js

loop: 200, immer avg spend 8.44 ms
loop: 200, limu avg spend 5.855 ms
loop: 200, mutative avg spend 5.525 ms
loop: 200, pstr avg spend 10.18 ms
loop: 200, native avg spend 0.895 ms

3 ST=3 node caseReadWrite.js

loop: 200, immer avg spend 10.025 ms
loop: 200, limu avg spend 0.89 ms
loop: 200, mutative avg spend 0.705 ms
loop: 200, pstr avg spend 8.155 ms
loop: 200, native avg spend 0.155 ms

4 ST=4 node caseReadWrite.js

loop: 200, immer avg spend 11.025 ms
loop: 200, limu avg spend 1.61 ms
loop: 200, mutative avg spend 1.345 ms
loop: 200, pstr avg spend 9.225 ms
loop: 200, native avg spend 0.915 ms

关于limu

limu 是一个基于读时浅复制,写时标记修改的不可变js库,基于这种机制对调试更友好,可以复制以下代码到控制台体验

比如先访问unpkg,右键打开控制台,然后粘贴以下代码载入js

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

loadJs('https://unpkg.com/limu@3.2.2/dist/limu.min.js');
// loadJs('https://unpkg.com/immer@9.0.21/dist/immer.umd.production.min.js');

然后运行测试代码

lib = window.LimuApi;
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;
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

可观察更高将大大提高开发调试体验,如下图所示,展开limu的draft后可实时观察draft所有数据节点

image

而 immer 展开就是这样的了

image

我的结论是追求极致性能mutative的确更好,追求调试友好limu更佳,不管怎样,在关闭冻结的情况下,两者均超immer数倍,对mutative在不可变数据做出的探索 respect ∠(°ゝ°) ❤️

fantasticsoul commented 11 months ago

Is immer performance issue still a thing? Because link performance issue from readme opens 0 (zero) issues.

see the benchmark of limu, it is also very fast.

unadlib commented 11 months ago

Thanks to benchmarking, and we are about to release v1.0.

udas0674 commented 9 months ago

Hello: When is the v1.0, production version getting released?

unadlib commented 9 months ago

Hello: When is the v1.0, production version getting released?

Currently, there will be no updates to the code or features, and I'm shifting my focus solely onto the documentation. It is very likely that the revised documentation will be released in early October.

udas0674 commented 9 months ago

Thank you!

udas0674 commented 8 months ago

Hello: Our team is interested in taking up current version (which is not v1.0) yet. Is it stable enough to use or should we wait till V1.0 is released? Will v1.0 be released somewhere in October 2023?

unadlib commented 8 months ago

Hello: Our team is interested in taking up current version (which is not v1.0) yet. Is it stable enough to use or should we wait till V1.0 is released? Will v1.0 be released somewhere in October 2023?

It's stable enough, with mutative having a 100% test coverage, and it passes all of Immer's test cases. Mutative has also completely fixed the potential issues found in Immer.

We've already released v0.6.0 two months ago, and we are anticipating the release of v1.0 possibly in October. Currently, we are just waiting to finish up the documentation, as the code has been frozen, meaning that the code for v1.0 will be identical to that of v0.6.0.

Zerebokep commented 8 months ago

Keep up the good work, I'm planning to suggest mutative for our agencies next large project.

fantasticsoul commented 8 months ago

Hello: Our team is interested in taking up current version (which is not v1.0) yet. Is it stable enough to use or should we wait till V1.0 is released? Will v1.0 be released somewhere in October 2023?

try limu,it is now the fastest immutable data js lib,and it is also production environment available,our new web new.qq.com is using it.

//You can verify through the following tests, and you are also welcome to add more performance test cases

git checkout https://github.com/tnfe/limu.git
cd benchmark
npm i
## Then execute the following commands separately
npm run s1
npm run s2
npm run s3
npm run s4

The results of one set of performance tests image

At the same time, the number of single test cases has increased to 500+, with a coverage rate of '95+' (the results can be viewed by executing NPM run tests in the root directory) image

unadlib commented 6 months ago

Hi all, we've released Mutative 1.0. Feel free to use it.