Created
December 21, 2014 02:39
-
-
Save Pauan/ea872c10d32d8d11ebd0 to your computer and use it in GitHub Desktop.
Benchmarks for Facebook's Immutable-js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You will need to use `npm install benchmark immutable`. Then do `node Immutable-js`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require("benchmark"); | |
var suite = new Benchmark.Suite(); | |
function rpad(x, i) { | |
return (x + new Array(i + 1).join(" ")).slice(0, i); | |
} | |
function lpad(x, i) { | |
return (new Array(i + 1).join(" ") + x).slice(-i); | |
} | |
suite.on("complete", function () { | |
var re = /([^\n]+)( x )([\d,]+)( ops\/sec ±[\d.]+% \(\d+ runs sampled\))/g; | |
var s = this.join("\n"); | |
var a; | |
var out = []; | |
var lMax = 0; | |
var rMax = 0; | |
while ((a = re.exec(s)) !== null) { | |
lMax = Math.max(lMax, a[1].length); | |
rMax = Math.max(rMax, a[3].length); | |
out.push([a[1], a[2], a[3], a[4]]); | |
} | |
console.log(out.map(function (a) { | |
return rpad(a[0], lMax) + a[1] + lpad(a[2], rMax) + a[3] | |
}).join("\n")); | |
}); | |
exports.time = function (s, f) { | |
suite.add(s, f); | |
}; | |
exports.run = function () { | |
suite.run(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var benchmark = require("./benchmark"); | |
var immutable = require("immutable"); | |
function generateKeys(counter) { | |
var keys = []; | |
while (counter--) { | |
keys.splice(Math.floor(Math.random() * keys.length), 0, "foo" + counter); | |
} | |
return keys; | |
} | |
function copy(o) { | |
var p = {}; | |
for (var s in o) { | |
p[s] = o[s]; | |
} | |
return p; | |
} | |
var suites = {}; | |
suites["object get"] = function (counter) { | |
var keys = generateKeys(counter); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
benchmark.time("Mutable object", function () { | |
o[keys[0]]; | |
}); | |
})(); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
Object.freeze(o); | |
benchmark.time("Frozen object", function () { | |
o[keys[0]]; | |
}); | |
})(); | |
;(function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
benchmark.time("Immutable-js Map", function () { | |
o.get(keys[0]); | |
}); | |
})(); | |
}; | |
suites["object insert"] = function (counter) { | |
var keys = generateKeys(counter); | |
benchmark.time("Mutable object", function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
}); | |
benchmark.time("Mutable object copying", function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
var p = copy(o); | |
p[key] = true; | |
o = p; | |
}); | |
}); | |
benchmark.time("Frozen object copying", function () { | |
var o = {}; | |
Object.freeze(o); | |
keys.forEach(function (key) { | |
var p = copy(o); | |
p[key] = true; | |
Object.freeze(p); | |
o = p; | |
}); | |
}); | |
benchmark.time("Immutable-js Map", function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
}); | |
benchmark.time("Immutable-js Map Transient", function () { | |
var o = immutable.Map(); | |
o = o.withMutations(function (o) { | |
keys.forEach(function (key) { | |
o.set(key, true); | |
}); | |
}); | |
}); | |
}; | |
suites["object remove"] = function (counter) { | |
var keys = generateKeys(counter); | |
benchmark.time("Mutable object", function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
keys.forEach(function (key) { | |
delete o[key]; | |
}); | |
}); | |
benchmark.time("Mutable object copying", function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
keys.forEach(function (key) { | |
var p = copy(o); | |
delete p[key]; | |
o = p; | |
}); | |
}); | |
benchmark.time("Frozen object copying", function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
Object.freeze(o); | |
keys.forEach(function (key) { | |
var p = copy(o); | |
delete p[key]; | |
Object.freeze(p); | |
o = p; | |
}); | |
}); | |
benchmark.time("Immutable-js Map", function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
keys.forEach(function (key) { | |
o = o.remove(key); | |
}); | |
}); | |
benchmark.time("Immutable-js Map Transient", function () { | |
var o = immutable.Map(); | |
o = o.withMutations(function (o) { | |
keys.forEach(function (key) { | |
o.set(key, true); | |
}); | |
}); | |
o = o.withMutations(function (o) { | |
keys.forEach(function (key) { | |
o.remove(key); | |
}); | |
}); | |
}); | |
}; | |
suites["object update"] = function (counter) { | |
var keys = generateKeys(counter); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
benchmark.time("Mutable object", function () { | |
keys.forEach(function (key) { | |
o[key] = false; | |
}); | |
}); | |
})(); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
benchmark.time("Mutable object copying", function () { | |
keys.forEach(function (key) { | |
var p = copy(o); | |
p[key] = false; | |
o = p; | |
}); | |
}); | |
})(); | |
;(function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
benchmark.time("Immutable-js Map", function () { | |
var p = o; | |
keys.forEach(function (key) { | |
p = p.set(key, false); | |
}); | |
}); | |
})(); | |
;(function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
benchmark.time("Immutable-js Map Transient", function () { | |
o.withMutations(function (o) { | |
keys.forEach(function (key) { | |
o.set(key, false); | |
}); | |
}); | |
}); | |
})(); | |
}; | |
suites["record"] = function (counter) { | |
var keys = generateKeys(counter); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
benchmark.time("Mutable object", function () { | |
o[keys[0]] = false; | |
}); | |
})(); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
benchmark.time("Mutable object copying", function () { | |
var p = copy(o); | |
p[keys[0]] = false; | |
}); | |
})(); | |
;(function () { | |
var o = {}; | |
keys.forEach(function (key) { | |
o[key] = true; | |
}); | |
Object.freeze(o); | |
benchmark.time("Frozen object copying", function () { | |
var p = copy(o); | |
p[keys[0]] = false; | |
Object.freeze(p); | |
}); | |
})(); | |
;(function () { | |
var o = immutable.Map(); | |
keys.forEach(function (key) { | |
o = o.set(key, true); | |
}); | |
benchmark.time("Immutable-js Map", function () { | |
o.set(keys[0], false); | |
}); | |
})(); | |
}; | |
var keys = 1; | |
//suites["object get"](keys); | |
//suites["object insert"](keys); | |
//suites["object remove"](keys); | |
//suites["object update"](keys); | |
suites["record"](keys); | |
benchmark.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment