Created
July 5, 2016 14:35
-
-
Save jorendorff/fe5cb03c29725c8a156400a629417c9d 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
<!doctype html> | |
<html> | |
<head> | |
<title>js speed test</title> | |
</head> | |
<body> | |
<script> | |
{ | |
let a = []; | |
for (let i = 0; i < 1000000; i++) | |
a[i] = i; | |
let results = []; | |
for (let n = 0; n < 80; n++) { | |
let s = 0; | |
let t0 = Date.now(); | |
for (let i = 0; i < a.length; i++) | |
s += a[i]; | |
let t1 = Date.now(); | |
if (s != a.length * (a.length - 1) / 2) | |
throw "FAIL"; | |
results.push(t1 - t0); | |
} | |
let results2 = []; | |
for (let n = 0; n < 80; n++) { | |
let s = 0; | |
let t0 = Date.now(); | |
a.forEach(function (v) { s += v; }); | |
let t1 = Date.now(); | |
if (s != a.length * (a.length - 1) / 2) | |
throw "FAIL"; | |
results2.push(t1 - t0); | |
} | |
document.write("<P>" + results); | |
document.write("<P>" + results2); | |
} | |
</script> | |
</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
{ | |
let a = []; | |
for (let i = 0; i < 1000000; i++) | |
a[i] = i; | |
let results = []; | |
for (let n = 0; n < 80; n++) { | |
let s = 0; | |
let t0 = Date.now(); | |
for (let i = 0; i < a.length; i++) | |
s += a[i]; | |
let t1 = Date.now(); | |
if (s != a.length * (a.length - 1) / 2) | |
throw "FAIL"; | |
results.push(t1 - t0); | |
} | |
let results2 = []; | |
for (let n = 0; n < 80; n++) { | |
let s = 0; | |
let t0 = Date.now(); | |
a.forEach(function (v) { s += v; }); | |
let t1 = Date.now(); | |
if (s != a.length * (a.length - 1) / 2) | |
throw "FAIL"; | |
results2.push(t1 - t0); | |
} | |
print(results); | |
print(results2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment