Created
July 12, 2017 14:30
-
-
Save jerch/04a0626ef72a23e943241fb1d94a4359 to your computer and use it in GitHub Desktop.
array vs typed vs asm (https://jsbench.github.io/#04a0626ef72a23e943241fb1d94a4359) #jsbench #jsperf
This file contains 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>array vs typed vs asm</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 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 l = 10|0; | |
let t = new Uint8Array(l+1); | |
for (let i=0; i<l; ++i) | |
t[i] = i; | |
function typedf() { | |
for (let i=0; i<l; ++i) { | |
t[l] = t[i]; | |
t[i] = t[l-1-i]; | |
t[l-1-i] = t[l]; | |
} | |
} | |
let a = []; | |
for (let i=0; i<l; ++i) | |
a.push(i); | |
function arrayf() { | |
for (let i=0; i<l; ++i) { | |
let temp = a[i]; | |
a[i] = a[l-1-i]; | |
a[l-1-i] = temp; | |
} | |
} | |
function asm(std, heap) { | |
"use asm"; | |
var l = 10|0; | |
var at = new std.Uint8Array(heap); | |
var j = 0|0; | |
for (j=0|0; j<l; j=(j+1)|0) | |
at[j] = j|0; | |
function f() { | |
var i = 0; | |
var l = l|0; | |
for (i=0|0; i<l; i=(i+1)|0) { | |
at[l] = at[i]|0; | |
at[i] = at[l-1-i]|0; | |
at[l-1-i] = at[l]|0; | |
} | |
} | |
return { f: f }; | |
} | |
var asmf = asm({Uint8Array: Uint8Array}, new ArrayBuffer(1000)).f; | |
}; | |
suite.add("arrayf();", function () { | |
arrayf(); | |
}); | |
suite.add("typedf();", function () { | |
typedf(); | |
}); | |
suite.add("asmf();", function () { | |
asmf(); | |
}); | |
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("array vs typed vs asm"); | |
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