jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Compare native Map implementation vs using an object as a hash map. Added 2 optimized tests for iterating the hash map.
<script>
;(function(window, floor, random){'use strict';
var pc = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").split('');
var len = pc.length;
function randomString() {
/// Will do this 5 times
return pc[floor(random() * len)]
+ pc[floor(random() * len)]
+ pc[floor(random() * len)]
+ pc[floor(random() * len)]
+ pc[floor(random() * len)]
;
}
window.randomString = randomString;
})(window, Math.floor, Math.random);
;(function(window){'use strict';
function EnumVal(name) { this.name = name; }
EnumVal.prototype.toString = function enumValToString() { return this.name; };
EnumVal.prototype.toJSON = function enumValToJSON() { return {name: this.name}; };
window.EnumVal = EnumVal;
})(window);
;(function(window, Map, EnumVal, random, randomString){'use strict';
function createNewJsMap(iterableObj) {
return iterableObj ? new Map(iterableObj) : new Map();
}
function createNullProtoObject() { return Object.create(null); }
function generateArrayOfTuples() {
var i = 0;
var tupleArray = [];
do {
tupleArray.push([randomString(), randomString()]);
i += 1;
} while(i < 1000);
return tupleArray;
}
function __mapCb_addRandom(el) {
return [random(),el]; /// randomAndEl
}
function __mapCb_pluckKey(randomAndEl) {
return randomAndEl[1][0];
}
function __sortCb_byFirstEl(randomAndEl_a, randomAndEl_b) {
return randomAndEl_a[0] < randomAndEl_b[0];
}
function __mapCb_keyToEnumVal(el) {
return [new EnumVal(el[0]), el[1]];
}
function shuffledArrayOfTuples(tuplesArray) {
return tuplesArray
.map(__mapCb_addRandom)
.sort(__sortCb_byFirstEl)
.map(__mapCb_pluckKey)
}
function enumeratedKeysForArrayOfTuples(tuplesArray) {
return tuplesArray
.map(__mapCb_keyToEnumVal)
;
}
window.createNewJsMap = createNewJsMap;
window.createNullProtoObject = createNullProtoObject;
window.generateArrayOfTuples = generateArrayOfTuples;
window.shuffledArrayOfTuples = shuffledArrayOfTuples;
window.enumeratedKeysForArrayOfTuples = enumeratedKeysForArrayOfTuples;
})(window, window.Map, window.EnumVal, Math.random, window.randomString);
</script>
var arrayOfTuples = generateArrayOfTuples();
var enumTuples = enumeratedKeysForArrayOfTuples(arrayOfTuples);
var map = window.createNewJsMap();
var map0 = window.createNewJsMap();
var map2 = window.createNewJsMap(arrayOfTuples);
var map3 = window.createNewJsMap(enumTuples);
var obj = window.createNullProtoObject();
var obj2 = window.createNullProtoObject();
var obj3 = window.createNullProtoObject();
var pkeys = shuffledArrayOfTuples(arrayOfTuples);
var pkeysEnum = shuffledArrayOfTuples(enumTuples);
for (var i=0, L=arrayOfTuples.length; i < L; i+=1) {
obj2[arrayOfTuples[0]] = arrayOfTuples[1];
obj3[enumTuples[0].toString()] = enumTuples[1];
}
Ready to run.
Test | Ops/sec | |
---|---|---|
map.set(key,val) |
| ready |
map.set(enumValKey, val) |
| ready |
obj[key] = val; |
| ready |
obj[enumValKey] = val; |
| ready |
Iterate Map |
| ready |
Iterate Map EnumVal |
| ready |
Iterate Object |
| ready |
Iterate Object Keys |
| ready |
Iterate Object Keys EnumVal |
| ready |
Iterate Object no hasOwnProperty check |
| ready |
Iterate Object no hasOwnProperty check EnumVal |
| ready |
Iterate Object Precomputed Keys |
| ready |
Iterate Object Precomputed EnumVal Keys |
| ready |
Iterate Map Precomputed Keys |
| ready |
Iterate Map Precomputed EnumVal Keys |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.