immutable.js 比较/等于嵌套地图/对象的性能?
immutable.js performance with comparison / equals against nested maps / objects?
我一直在尝试查找有关 immutable.js 在与深层/嵌套对象进行比较时如何执行的信息,特别是在 big-o 表示法方面。有人碰巧知道它是什么吗?
objA = Immutable.Map({
a: 'test',
b: Immutable.Map({ ... })
});
objB = Immutable.Map({
a: 'test',
b: Immutable.Map({ ... })
});
objA.equals(objB); // not clear what the perf is on this
看看这是否有帮助:
const Immutable = require('immutable')
function test (limit, mutate) {
const rawA = {}
var i
for (i = 0; i < limit; i++) {
rawA[String(i)] = i
}
const rawB = {}
for (i = 0; i < limit; i++) {
rawB[String(i)] = i
}
if (mutate) {
rawB['poison'] = false
}
if (Object.keys(rawA).length && Object.keys(rawB).length) {
if (false) {
console.log('dummy')
}
}
const a = Immutable.Map(rawA)
const b = Immutable.Map(rawB)
const timer = process.hrtime()
if (a.equals(b)) {
if (false) {
console.log('dummy')
}
}
return process.hrtime(timer)
}
const sizes = [1, 10, 100, 1000, 10 * 1000, 100 * 1000, 200 * 1000, 400 * 1000, 500 * 1000]
sizes.forEach((limit) => {
var t1 = 0
var t2 = 0
const tries = [1, 2, 3, 4, 5]
tries.forEach((i) => {
t1 += test(limit, true)[1]
t2 += test(limit, false)[1]
})
console.log(limit,
t1,
Math.round(t1 / (tries.length * limit)),
t2,
Math.round(t2 / (tries.length * limit)))
})
粗略地说,当地图不相等时,.equals
的性能是 O(1),当它们相等时,不比 O(n) 差。
1 313329 62666 579815 115963
10 9617 192 293337 5867
100 10873 22 1900225 3800
1000 17505 4 4998441 1000
10000 32060 1 12044412 241
100000 81504 0 211355401 423
200000 99704 0 543211687 543
400000 133836 0 2231794320 1116
500000 77741 0 2877654681 1151
我一直在尝试查找有关 immutable.js 在与深层/嵌套对象进行比较时如何执行的信息,特别是在 big-o 表示法方面。有人碰巧知道它是什么吗?
objA = Immutable.Map({
a: 'test',
b: Immutable.Map({ ... })
});
objB = Immutable.Map({
a: 'test',
b: Immutable.Map({ ... })
});
objA.equals(objB); // not clear what the perf is on this
看看这是否有帮助:
const Immutable = require('immutable')
function test (limit, mutate) {
const rawA = {}
var i
for (i = 0; i < limit; i++) {
rawA[String(i)] = i
}
const rawB = {}
for (i = 0; i < limit; i++) {
rawB[String(i)] = i
}
if (mutate) {
rawB['poison'] = false
}
if (Object.keys(rawA).length && Object.keys(rawB).length) {
if (false) {
console.log('dummy')
}
}
const a = Immutable.Map(rawA)
const b = Immutable.Map(rawB)
const timer = process.hrtime()
if (a.equals(b)) {
if (false) {
console.log('dummy')
}
}
return process.hrtime(timer)
}
const sizes = [1, 10, 100, 1000, 10 * 1000, 100 * 1000, 200 * 1000, 400 * 1000, 500 * 1000]
sizes.forEach((limit) => {
var t1 = 0
var t2 = 0
const tries = [1, 2, 3, 4, 5]
tries.forEach((i) => {
t1 += test(limit, true)[1]
t2 += test(limit, false)[1]
})
console.log(limit,
t1,
Math.round(t1 / (tries.length * limit)),
t2,
Math.round(t2 / (tries.length * limit)))
})
粗略地说,当地图不相等时,.equals
的性能是 O(1),当它们相等时,不比 O(n) 差。
1 313329 62666 579815 115963
10 9617 192 293337 5867
100 10873 22 1900225 3800
1000 17505 4 4998441 1000
10000 32060 1 12044412 241
100000 81504 0 211355401 423
200000 99704 0 543211687 543
400000 133836 0 2231794320 1116
500000 77741 0 2877654681 1151