-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b743c3
commit 445da92
Showing
6 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
;(function () { | ||
'use strict' | ||
|
||
let ol | ||
function runBenchmark (name, test) { | ||
if (!ol) { | ||
ol = document.createElement('ol') | ||
document.body.appendChild(ol) | ||
} | ||
|
||
setTimeout(function () { | ||
const start = new Date().getTime() | ||
test() | ||
const total = new Date().getTime() - start | ||
const li = document.createElement('li') | ||
li.innerHTML = `${name}: ${total} ms` | ||
ol.appendChild(li) | ||
}, 15) | ||
} | ||
|
||
const loopLength = 500000 | ||
|
||
let array = [] | ||
|
||
for (let i = 0; i < loopLength; i++) { | ||
array[i] = 'item' + i | ||
} | ||
|
||
function forLoop$1 () {} | ||
|
||
function forLoopCachedLength$1 () {} | ||
|
||
function forLoopDirectAccess$1 () {} | ||
|
||
function whileLoop$1 () {} | ||
|
||
function whileLoopCachedLength$1 () {} | ||
|
||
function reversedWhileLoop$1 () {} | ||
|
||
function doubleReversedWhileLoop$1 () {} | ||
|
||
var tests = Object.freeze({ | ||
forLoop: forLoop$1, | ||
forLoopCachedLength: forLoopCachedLength$1, | ||
forLoopDirectAccess: forLoopDirectAccess$1, | ||
whileLoop: whileLoop$1, | ||
whileLoopCachedLength: whileLoopCachedLength$1, | ||
reversedWhileLoop: reversedWhileLoop$1, | ||
doubleReversedWhileLoop: doubleReversedWhileLoop$1 | ||
}) | ||
|
||
const { | ||
forLoop, | ||
forLoopCachedLength, | ||
forLoopDirectAccess, | ||
whileLoop, | ||
whileLoopCachedLength, | ||
reversedWhileLoop, | ||
doubleReversedWhileLoop | ||
} = tests | ||
|
||
runBenchmark('for-loop', forLoop) | ||
runBenchmark('for-loop, cached length', forLoopCachedLength) | ||
runBenchmark('for-loop, direct array access', forLoopDirectAccess) | ||
runBenchmark('while-loop', whileLoop) | ||
runBenchmark('while-loop, cached length property', whileLoopCachedLength) | ||
runBenchmark('reversed while-loop', reversedWhileLoop) | ||
runBenchmark('double reversed while-loop', doubleReversedWhileLoop) | ||
})() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Relative performance of loops</title> | ||
</head> | ||
<body> | ||
<h1>Relative Performance of loops</h1> | ||
<ol></ol> | ||
<script type="text/javascript" src="./bundle.js"></script> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { runBenchmark, benchmark } from './lib/index' | ||
import * as tests from './loops' | ||
|
||
const { | ||
forLoop, | ||
forLoopCachedLength, | ||
forLoopDirectAccess, | ||
whileLoop, | ||
whileLoopCachedLength, | ||
reversedWhileLoop, | ||
doubleReversedWhileLoop | ||
} = tests | ||
|
||
benchmark('loops micro benchmark', [ | ||
forLoop, | ||
forLoopCachedLength, | ||
forLoopDirectAccess, | ||
whileLoop, | ||
whileLoopCachedLength, | ||
reversedWhileLoop, | ||
doubleReversedWhileLoop | ||
]) | ||
|
||
runBenchmark('for-loop', forLoop) | ||
runBenchmark('for-loop, cached length', forLoopCachedLength) | ||
runBenchmark('for-loop, direct array access', forLoopDirectAccess) | ||
runBenchmark('while-loop', whileLoop) | ||
runBenchmark('while-loop, cached length property', whileLoopCachedLength) | ||
runBenchmark('reversed while-loop', reversedWhileLoop) | ||
runBenchmark('double reversed while-loop', doubleReversedWhileLoop) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
let ol | ||
export function runBenchmark (name, test) { | ||
if (!ol) { | ||
ol = document.createElement('ol') | ||
document.body.appendChild(ol) | ||
} | ||
|
||
setTimeout(function () { | ||
const start = new Date().getTime() | ||
test() | ||
const total = new Date().getTime() - start | ||
const li = document.createElement('li') | ||
li.innerHTML = `${name}: ${total} ms` | ||
ol.appendChild(li) | ||
}, 15) | ||
} | ||
|
||
function init (name) { | ||
const heading = document.createElement('h2') | ||
const ol = document.createElement('ol') | ||
|
||
heading.innerHTML = name | ||
document.body.appendChild(heading) | ||
|
||
document.body.appendChild(ol) | ||
return ol | ||
} | ||
|
||
function runTests (tests, view, iterations) { | ||
for (let label in tests) { | ||
if (!tests.hasOwnProperty(label) || !typeof tests.label === 'function') { | ||
continue | ||
} | ||
|
||
;(function (name, test) { | ||
setTimeout(function () { | ||
const start = new Date().getTime() | ||
let l = iterations | ||
if (!test.length) { | ||
while (l--) { | ||
test() | ||
} | ||
} else { | ||
test(1) | ||
} | ||
|
||
const total = new Date().getTime() - start | ||
const li = document.createElement('li') | ||
li.innerHTML = `${name}: total ms (total) ${total / | ||
iterations} ms (avg)` | ||
view.appendChild(li) | ||
}, 15) | ||
})(label, tests[label]) | ||
} | ||
} | ||
|
||
export function benchmark (name, tests, iterations = 1000) { | ||
const view = init(name) | ||
runTests(tests, view, iterations) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const loopLength = 500000000 | ||
|
||
let array = [] | ||
|
||
for (let i = 0; i < loopLength; i++) { | ||
array[i] = 'item' + i | ||
} | ||
|
||
export function forLoop () { | ||
for (let i = 0, item; i < array.length; i++) { | ||
item = array[i] | ||
} | ||
} | ||
|
||
forLoop.label = 'for-loop' | ||
|
||
export function forLoopCachedLength () { | ||
for (let i = 0, l = array.length, item; i < l; i++) { | ||
item = array[i] | ||
} | ||
} | ||
|
||
forLoopCachedLength.label = 'forLoopCachedLength' | ||
|
||
export function forLoopDirectAccess () { | ||
for (let i = 0, item; (item = array[i]); i++) {} | ||
} | ||
|
||
forLoopDirectAccess.label = 'forLoopDirectAccess' | ||
|
||
export function whileLoop () { | ||
let i = 0 | ||
while (i < array.length) { | ||
const item = array[i] | ||
i++ | ||
} | ||
} | ||
|
||
whileLoop.label = 'whileLoop' | ||
|
||
export function whileLoopCachedLength () { | ||
let i = 0 | ||
const l = array.length | ||
while (i < l) { | ||
const item = array[i] | ||
i++ | ||
} | ||
} | ||
|
||
export function reversedWhileLoop () { | ||
let l = array.length | ||
while (l--) { | ||
const item = array[l] | ||
} | ||
} | ||
|
||
export function doubleReversedWhileLoop () { | ||
const l = array.length | ||
let i = l | ||
while (i--) { | ||
const item = array[l - i - 1] | ||
} | ||
} | ||
|
||
whileLoopCachedLength.label = 'whileLoopCachedLength' |
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