Created
July 11, 2017 17:23
-
-
Save JobLeonard/5a054fcfadc140bff6d563010c9fccc2 to your computer and use it in GitHub Desktop.
object vs two array vs inlined ASCII array insertion (http://jsbench.github.io/#5a054fcfadc140bff6d563010c9fccc2) #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 vs inlined ASCII array insertion</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", function () { | |
// ALL UTF16 object | |
let v = {}; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
v[i] = i; | |
} | |
} | |
}); | |
suite.add("ALL UTF16 double array", function () { | |
// ALL UTF16 double array | |
let v = []; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
let t = v[i>>>8]; | |
if (!t){ | |
t = []; | |
v[i>>>8] = t; | |
} | |
t[i&0xFF] = i; | |
} | |
} | |
}); | |
suite.add("ALL UTF16 inlined ASCII array", function () { | |
// ALL UTF16 inlined ASCII array | |
let v = []; | |
for (let j = 0; j < 10; j++){ | |
for (let i = 0; i < 256*256; i++){ | |
if (i < 256){ | |
v[i] = i; | |
} else { | |
let t = v[255 + (i>>>8)]; | |
if (!t){ | |
t = []; | |
v[255 + (i>>>8)] = t; | |
} | |
t[i&0xFF] = i; | |
} | |
} | |
} | |
}); | |
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 vs inlined ASCII array insertion"); | |
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