Skip to content

Instantly share code, notes, and snippets.

@d4tocchini
Last active June 7, 2017 00:38
Show Gist options
  • Save d4tocchini/3127ca10954b5121df21352e9baf8d05 to your computer and use it in GitHub Desktop.
Save d4tocchini/3127ca10954b5121df21352e9baf8d05 to your computer and use it in GitHub Desktop.
typed array or not, with or without allocating size #jsbench #jsperf (http://jsbench.github.io/#3127ca10954b5121df21352e9baf8d05) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>typed array or not, with or without allocating size #jsbench #jsperf</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>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
suite.add("if (!window.xA) {window.xA = []}", function () {
if (!window.xA) {window.xA = []}
var x = xA
var i = 0
for (i = 0; i<255; i++){
x[i] = i
}
for (i; i<510; i++){
x[i] = i - 255
}
var result
for (i = 0; i<255; i++){
result = x[i]
}
});
suite.add("if (!window.x8_65536) {window.x8_65536 = new Uint8Array(65536)}", function () {
if (!window.x8_65536) {window.x8_65536 = new Uint8Array(65536)}
var x = x8_65536
var i = 0
for (i = 0; i<255; i++){
x[i] = i
}
for (i; i<510; i++){
x[i] = i - 255
}
var result
for (i = 0; i<255; i++){
result = x[i]
}
});
suite.add("if (!window.x8_1024) {window.x8_1024 = new Uint8Array(1024)}", function () {
if (!window.x8_1024) {window.x8_1024 = new Uint8Array(1024)}
var x = x8_1024
var i = 0
for (i = 0; i<255; i++){
x[i] = i
}
for (i; i<510; i++){
x[i] = i - 255
}
var result
for (i = 0; i<255; i++){
result = x[i]
}
});
suite.add("if (!window.x8) {window.x8 = new Uint8Array()}", function () {
if (!window.x8) {window.x8 = new Uint8Array()}
var x = x8
var i = 0
for (i = 0; i<255; i++){
x[i] = i
}
for (i; i<510; i++){
x[i] = i - 255
}
var result
for (i = 0; i<255; i++){
result = x[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("typed array or not, with or without allocating size #jsbench #jsperf");
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