From 8b75639a4d48e517856d23f4bd0873cd50f9a619 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 7 Jul 2016 16:31:19 +1000 Subject: [PATCH 1/3] benchmark: add microbenchmarks for ES Map --- benchmark/es/map-bench.js | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 benchmark/es/map-bench.js diff --git a/benchmark/es/map-bench.js b/benchmark/es/map-bench.js new file mode 100644 index 00000000000000..ccb87634af6cb6 --- /dev/null +++ b/benchmark/es/map-bench.js @@ -0,0 +1,96 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + method: ['object', 'nullProtoObject', 'fakeMap', 'map'], + millions: [10] +}); + +function runObject(n) { + const m = {}; + var i = 0; + bench.start(); + for (; i < n; i++) { + m['i' + n] = n; + m['s' + n] = String(n); + assert.equal(m['i' + n], m['s' + n]); + m['i' + n] = undefined; + m['s' + n] = undefined; + } + bench.end(n / 1e6); +} + +function runNullProtoObject(n) { + const m = Object.create(null); + var i = 0; + bench.start(); + for (; i < n; i++) { + m['i' + n] = n; + m['s' + n] = String(n); + assert.equal(m['i' + n], m['s' + n]); + m['i' + n] = undefined; + m['s' + n] = undefined; + } + bench.end(n / 1e6); +} + +function fakeMap() { + const m = {}; + return { + get(key) { return m['$' + key]; }, + set(key, val) { m['$' + key] = val; }, + get size() { return Object.keys(m).length; }, + has(key) { return Object.prototype.hasOwnProperty.call(m, '$' + key); } + }; +} + +function runFakeMap(n) { + const m = fakeMap(); + var i = 0; + bench.start(); + for (; i < n; i++) { + m.set('i' + n, n); + m.set('s' + n, String(n)); + assert.equal(m.get('i' + n), m.get('s' + n)); + m.set('i' + n, undefined); + m.set('s' + n, undefined); + } + bench.end(n / 1e6); +} + +function runMap(n) { + const m = new Map(); + var i = 0; + bench.start(); + for (; i < n; i++) { + m.set('i' + n, n); + m.set('s' + n, String(n)); + assert.equal(m.get('i' + n), m.get('s' + n)); + m.set('i' + n, undefined); + m.set('s' + n, undefined); + } + bench.end(n / 1e6); +} + +function main(conf) { + const n = +conf.millions * 1e6; + + switch (conf.method) { + case 'object': + runObject(n); + break; + case 'nullProtoObject': + runNullProtoObject(n); + break; + case 'fakeMap': + runFakeMap(n); + break; + case 'map': + runMap(n); + break; + default: + throw new Error('Unexpected method'); + } +} From 15bd927639dd0b84c9aa2700cb86b1a91f62b624 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 18 Oct 2016 14:11:14 +1100 Subject: [PATCH 2/3] squash --- benchmark/es/map-bench.js | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/benchmark/es/map-bench.js b/benchmark/es/map-bench.js index ccb87634af6cb6..8290e6a3813380 100644 --- a/benchmark/es/map-bench.js +++ b/benchmark/es/map-bench.js @@ -5,7 +5,7 @@ const assert = require('assert'); const bench = common.createBenchmark(main, { method: ['object', 'nullProtoObject', 'fakeMap', 'map'], - millions: [10] + thousands: [100] }); function runObject(n) { @@ -13,11 +13,11 @@ function runObject(n) { var i = 0; bench.start(); for (; i < n; i++) { - m['i' + n] = n; - m['s' + n] = String(n); - assert.equal(m['i' + n], m['s' + n]); - m['i' + n] = undefined; - m['s' + n] = undefined; + m['i' + i] = i; + m['s' + i] = String(i); + assert.equal(m['i' + i], m['s' + i]); + m['i' + i] = undefined; + m['s' + i] = undefined; } bench.end(n / 1e6); } @@ -27,11 +27,11 @@ function runNullProtoObject(n) { var i = 0; bench.start(); for (; i < n; i++) { - m['i' + n] = n; - m['s' + n] = String(n); - assert.equal(m['i' + n], m['s' + n]); - m['i' + n] = undefined; - m['s' + n] = undefined; + m['i' + i] = i; + m['s' + i] = String(i); + assert.equal(m['i' + i], m['s' + i]); + m['i' + i] = undefined; + m['s' + i] = undefined; } bench.end(n / 1e6); } @@ -51,11 +51,11 @@ function runFakeMap(n) { var i = 0; bench.start(); for (; i < n; i++) { - m.set('i' + n, n); - m.set('s' + n, String(n)); - assert.equal(m.get('i' + n), m.get('s' + n)); - m.set('i' + n, undefined); - m.set('s' + n, undefined); + m.set('i' + i, i); + m.set('s' + i, String(i)); + assert.equal(m.get('i' + i), m.get('s' + i)); + m.set('i' + i, undefined); + m.set('s' + i, undefined); } bench.end(n / 1e6); } @@ -65,17 +65,17 @@ function runMap(n) { var i = 0; bench.start(); for (; i < n; i++) { - m.set('i' + n, n); - m.set('s' + n, String(n)); - assert.equal(m.get('i' + n), m.get('s' + n)); - m.set('i' + n, undefined); - m.set('s' + n, undefined); + m.set('i' + i, i); + m.set('s' + i, String(i)); + assert.equal(m.get('i' + i), m.get('s' + i)); + m.set('i' + i, undefined); + m.set('s' + i, undefined); } bench.end(n / 1e6); } function main(conf) { - const n = +conf.millions * 1e6; + const n = +conf.thousands * 1000; switch (conf.method) { case 'object': From b550a78399e2d6ce295467dbc072790d814535c0 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 18 Oct 2016 21:02:50 +1100 Subject: [PATCH 3/3] squash, back up to 1 million --- benchmark/es/map-bench.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/es/map-bench.js b/benchmark/es/map-bench.js index 8290e6a3813380..574da25d53f2f2 100644 --- a/benchmark/es/map-bench.js +++ b/benchmark/es/map-bench.js @@ -5,7 +5,7 @@ const assert = require('assert'); const bench = common.createBenchmark(main, { method: ['object', 'nullProtoObject', 'fakeMap', 'map'], - thousands: [100] + millions: [1] }); function runObject(n) { @@ -75,7 +75,7 @@ function runMap(n) { } function main(conf) { - const n = +conf.thousands * 1000; + const n = +conf.millions * 1e6; switch (conf.method) { case 'object':