Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created July 5, 2016 14:35
Show Gist options
  • Save jorendorff/fe5cb03c29725c8a156400a629417c9d to your computer and use it in GitHub Desktop.
Save jorendorff/fe5cb03c29725c8a156400a629417c9d to your computer and use it in GitHub Desktop.
<!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>
{
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