Created
September 29, 2016 21:02
-
-
Save mwcz/d44a0fba1b732acb2c67297ad38ffffa to your computer and use it in GitHub Desktop.
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
<script src="typed.js"></script> |
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
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