Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mwcz
Created September 29, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwcz/d44a0fba1b732acb2c67297ad38ffffa to your computer and use it in GitHub Desktop.
Save mwcz/d44a0fba1b732acb2c67297ad38ffffa to your computer and use it in GitHub Desktop.
<script src="typed.js"></script>
const count = 10;
const arraySize = count * 6;
const runs = 1e4;
// set up typed array test
let a = new Float32Array(arraySize).map(Math.random);
let b = new Float32Array(arraySize).map(Math.random);
let c = new Float32Array(arraySize).map(Math.random);
function typedPerf() {
for(var i = 0; i < arraySize; ++i) {
b[i] = a[i];
c[i] = a[i];
}
}
// set up push/shift array test
let d = new Array(arraySize).fill(1).map(Math.random);
let e = new Array(arraySize).fill(1).map(Math.random);
let f = new Array(arraySize).fill(1).map(Math.random);
function pushPerf() {
for(var i = 0; i < 12; ++i) {
e.shift();
}
for(var i = 0; i < 12; ++i) {
e.push(d[i]);
}
for(var i = 0; i < 12; ++i) {
f.pop();
}
for(var i = 0; i < 12; ++i) {
f.unshift(d[i]);
}
}
function perf(fn, name) {
let avg = 0;
for(var i = 0; i < runs; ++i) {
a = new Float32Array(arraySize).map(Math.random);
b = new Float32Array(arraySize).map(Math.random);
c = new Float32Array(arraySize).map(Math.random);
d = new Array(arraySize).fill(1).map(Math.random);
e = new Array(arraySize).fill(1).map(Math.random);
f = new Array(arraySize).fill(1).map(Math.random);
let start = performance.now();
fn();
let end = performance.now();
let time = end - start;
avg += time;
}
avg /= runs;
console.log(`${name}: ${avg} ms`);
}
perf(typedPerf, 'typed array copy');
perf(pushPerf, 'array push/shift');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment