Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Map keyed by Object
Comparing different methods for using an arbitrary object as a Map key, for a relatively small number of entries. Used to make decisions about how to cache WrappedEnum instances in ts-enum-util: https://github.com/UselessPickles/ts-enum-util
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0.0
Browser:
Opera 107
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Polyfill Map by Object
1799202.5 Ops/sec
Native Map by Object
2049569.0 Ops/sec
Native Map by JSON
580146.6 Ops/sec
Object by JSON
566742.9 Ops/sec
Native WeakMap by Object
2173918.5 Ops/sec
Script Preparation code:
function SlowMap() { this._keys = []; this._values = []; } SlowMap.prototype = { set: function(key, value) { const index = this._keys.indexOf(key); if (index !== -1) { this._values[index] = value; } else { this._keys.push(key); this._values.push(value); } }, get: function(key) { const index = this._keys.indexOf(key); if (index !== -1) { return this._values[index]; } else { return undefined; } } }; var OBJECT_COUNT = 500; var PROPERTY_COUNT = 10; var OBJECTS = []; var slowMapByObj = new SlowMap(); var nativeMapByObj = new Map(); var nativeWeakMapByObj = new WeakMap(); var nativeMapByJson = new Map(); var objectMapByJson = {}; for (let i = 0; i < OBJECT_COUNT; ++i) { var obj = {}; for (let j = 0; j < PROPERTY_COUNT; ++j) { obj[i + "|" + j] = i * PROPERTY_COUNT + j; } OBJECTS.push(obj); slowMapByObj.set(obj, i); nativeMapByObj.set(obj, i); nativeWeakMapByObj.set(obj, i); nativeMapByJson.set(JSON.stringify(obj), i); objectMapByJson[JSON.stringify(obj)] = i; } var testLoopCount = 0;
Tests:
Polyfill Map by Object
var result = slowMapByObj.get(OBJECTS[testLoopCount++ % OBJECT_COUNT]);
Native Map by Object
var result = nativeMapByObj.get(OBJECTS[testLoopCount++ % OBJECT_COUNT]);
Native Map by JSON
var result = nativeMapByJson.get(JSON.stringify(OBJECTS[testLoopCount++ % OBJECT_COUNT]));
Object by JSON
var result = objectMapByJson[JSON.stringify(OBJECTS[testLoopCount++ % OBJECT_COUNT])];
Native WeakMap by Object
var result = nativeWeakMapByObj.get(OBJECTS[testLoopCount++ % OBJECT_COUNT]);