Last active
July 11, 2017 16:39
-
-
Save JobLeonard/f90bf23a39841a3da071c51af082aaab to your computer and use it in GitHub Desktop.
object vs two array lookups vs inlined ASCII array (http://jsbench.github.io/#f90bf23a39841a3da071c51af082aaab) #jsbench #jsperf
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>object vs two array lookups vs inlined ASCII array</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or 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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
let testObj = {}; | |
let nestedArray = new Array(256); | |
let inlinedArray = new Array(256+255); | |
let testAnswer = new Array(256*256); | |
for (let i = nestedArray.length; i--;){ | |
let subArray = new Array(256); | |
for (let j = subArray.length; j--;){ | |
let v = j + i*256; | |
testObj[v] = v; | |
testAnswer[v] = v; | |
subArray[j] = v; | |
if (i === 0){ | |
inlinedArray[j] = v; | |
} | |
} | |
if (i > 0){ | |
inlinedArray[i+255] = subArray; | |
} | |
nestedArray[i] = subArray; | |
} | |
// Test if all lookups would produce the correct answer | |
// Object lookup | |
for (let i = 0; i < 256*256; i++){ | |
let v = testObj[i]; | |
if (testAnswer[i] !== v) throw 'Object lookup mismatch, expected ' + testAnswer[i] + ', got ' + v; | |
} | |
// Double array lookup | |
for (let i = 0; i < 256*256; i++){ | |
let v = nestedArray[i>>>8][i&0xFF]; | |
if (testAnswer[i] !== v) throw 'double array lookup mismatch, expected ' + testAnswer[i] + ', got ' + v; | |
} | |
// Inlined ASCII array lookup | |
for (let i = 0; i < 256*256; i++){ | |
let v = i < 256 ? inlinedArray[i] : inlinedArray[255 + (i>>>8)][i&0xFF] | |
if (testAnswer[i] !== v) throw 'Inlined ASCII mismatch, expected ' + testAnswer[i] + ', got ' + v; | |
} | |
}; | |
suite.add("ALL UTF16 object lookup", function () { | |
// ALL UTF16 object lookup | |
let v = 0; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
v = testObj[i]; | |
} | |
} | |
}); | |
suite.add("ALL UTF16 double array lookup", function () { | |
// ALL UTF16 double array lookup | |
let v = 0; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
v = nestedArray[i>>>8][i&0xFF]; | |
} | |
} | |
}); | |
suite.add("ALL UTF16 inlined array lookup", function () { | |
// ALL UTF16 inlined array lookup | |
let v = 0; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
if (i < 256){ | |
v = inlinedArray[i]; | |
} else { | |
v = inlinedArray[255 + (i>>>8)][i&0xFF]; | |
} | |
} | |
} | |
}); | |
suite.add("ASCII/UTF16 9:1 object lookup", function () { | |
// ASCII/UTF16 9:1 object lookup | |
let v = 0; | |
for (let j = 0; j < 256*9; j++){ | |
for (let i = 0; i < 256; i++){ | |
v = testObj[i]; | |
} | |
} | |
for (let i = 0; i < 256*256; i++){ | |
v = testObj[i]; | |
} | |
}); | |
suite.add("ASCII/UTF16 9:1 double array lookup", function () { | |
// ASCII/UTF16 9:1 double array lookup | |
let v = 0; | |
for (let j = 0; j < 256*9; j++){ | |
for (let i = 0; i < 256; i++){ | |
v = nestedArray[i>>>8][i&0xFF]; | |
} | |
} | |
for (let i = 0; i < 256*256; i++){ | |
v = nestedArray[i>>>8][i&0xFF]; | |
} | |
}); | |
suite.add("ASCII/UTF16 9:1 inlined array lookup", function () { | |
// ASCII/UTF16 9:1 inlined array lookup | |
let v = 0; | |
for (let j = 0; j < 256*9; j++){ | |
for (let i = 0; i < 256; i++){ | |
if (i < 256){ | |
v = inlinedArray[i]; | |
} else { | |
v = inlinedArray[255 + (i>>>8)][i&0xFF]; | |
} | |
} | |
} | |
for (let i = 0; i < 256*256; i++){ | |
if (i < 256){ | |
v = inlinedArray[i]; | |
} else { | |
v = inlinedArray[255 + (i>>>8)][i&0xFF]; | |
} | |
} | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("object vs two array lookups vs inlined ASCII array"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment