Test name | Executions per second |
---|---|
Object random string keys | 1855.3 Ops/sec |
Object random numeric smi keys | 2278.4 Ops/sec |
Object sequential string keys | 2000.1 Ops/sec |
Object sequential numeric smi keys | 18084.4 Ops/sec |
Map random string keys | 1140.4 Ops/sec |
Map random numeric smi keys | 1511.4 Ops/sec |
Map sequential string keys | 1387.8 Ops/sec |
Map sequential numeric smi keys | 1419.8 Ops/sec |
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// 31 bit +ve integer smi
var randSmi = () => crypto.getRandomValues(new Uint32Array(1))[0] & 0x7fffffff;
// 6-7 char alphanumeric string
var randStr = () => randSmi().toString(36);
var numEntries = 64;
var numRounds = 100 * numEntries;
var strObj = {};
var strObjKeys = Array(numEntries);
var numObj = {};
var numObjKeys = Array(numEntries);
var strSeqObj = {};
var strSeqObjKeys = Array(numEntries);
var numSeqObj = {};
var numSeqObjKeys = Array(numEntries);
for (let i = 0; i < numEntries; i++) {
let strKey = `k${randStr()}`, numKey = randSmi();
let strSeqKey = `k${i}`, numSeqKey = i;
strObj[strKey] = i;
strObjKeys[i] = strKey;
numObj[numKey] = i;
numObjKeys[i] = numKey;
strSeqObj[strSeqKey] = i;
strSeqObjKeys[i] = strSeqKey;
numSeqObj[numSeqKey] = i;
numSeqObjKeys[i] = numSeqKey;
}
Object.freeze(strObj);
Object.freeze(numObj);
Object.freeze(strSeqObj);
Object.freeze(numSeqObj);
var strMap = new Map(Object.entries(strObj));
var numMap = new Map(Object.entries(numObj));
var strSeqMap = new Map(Object.entries(strSeqObj));
var numSeqMap = new Map(Object.entries(numSeqObj));
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += strObj[strObjKeys[Math.random() * numEntries | 0]];
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += numObj[numObjKeys[Math.random() * numEntries | 0]];
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += strSeqObj[strSeqObjKeys[Math.random() * numEntries | 0]];
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += numSeqObj[numSeqObjKeys[Math.random() * numEntries | 0]];
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += strMap.get(strObjKeys[Math.random() * numEntries | 0]);
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += numMap.get(numObjKeys[Math.random() * numEntries | 0]);
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += strSeqMap.get(strSeqObjKeys[Math.random() * numEntries | 0]);
}
let total = 0;
for (let i = 0; i < numRounds; i++) {
total += numSeqMap.get(numSeqObjKeys[Math.random() * numEntries | 0]);
}