gpt4 book ai didi

javascript - 为什么 2 个 immutable.js 列表有很大不同?

转载 作者:行者123 更新时间:2023-12-03 05:43:57 26 4
gpt4 key购买 nike

我无法使用简单的深度对象比较来深入比较 2 个列表。

考虑以下代码:

import {fromJS} from 'immutable';
import {describe, it} from 'mocha';
import { expect } from 'chai';

const o = {a: 123};
const A = fromJS([o]);
const B = fromJS([]).push(fromJS(o));

describe('Check', () => {
it('should be deep equal', () => {
expect(A).to.deep.equal(B);
});
});

为什么 Immutable.js 集合实际上有状态?

我看到像 chai-immutable 这样的库,但我想了解这种行为的目的?

最佳答案

Immutable.js 使用状态来提高效率,因此当您将某些内容添加到不可变列表中时,引用会有所不同,但它实际上不会复制所有内容并且时间复杂度为 O(n)。

正如他们的 Github 中所述:

These data structures are highly efficient on modern JavaScript VMs by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data.

关于javascript - 为什么 2 个 immutable.js 列表有很大不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com