Skip to content

Commit

Permalink
Update with shitty microbenchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsiampolu committed Nov 23, 2017
1 parent 5b743c3 commit 445da92
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 1 deletion.
70 changes: 70 additions & 0 deletions ch4/benchmarks/build/bundle.js
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)
})()
11 changes: 11 additions & 0 deletions ch4/benchmarks/build/index.html
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>
30 changes: 30 additions & 0 deletions ch4/benchmarks/index.js
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)
60 changes: 60 additions & 0 deletions ch4/benchmarks/lib/index.js
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)
}
66 changes: 66 additions & 0 deletions ch4/benchmarks/loops.js
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'
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"http-server": "^0.10.0",
"husky": "^0.14.3",
"lint-staged": "^5.0.0",
"open-cli": "^1.0.5",
"prettier-standard": "^7.0.3",
"rollup": "^0.51.5",
"standard": "^10.0.3"
Expand All @@ -16,10 +17,13 @@
"precommit": "lint-staged",
"build:ch1": "rollup ./ch1/strftime.test.js --o ./ch1/public/bundle.js --f iife",
"build:ch2": "rollup ./ch2/trim.test.js --o ./ch2/public/bundle.js --f iife",
"build:ch4": "rollup ./ch4/benchmarks/index.js --o ./ch4/benchmarks/build/bundle.js --f iife",
"serve:ch1": "http-server ./ch1/public -p 3020",
"serve:ch2": "http-server ./ch2/public -p 3020",
"serve:ch4": "http-server ./ch4/benchmarks/build -p 3020",
"start:ch1": "yarn build:ch1 && yarn serve:ch1",
"start:ch2": "yarn build:ch2 && yarn serve:ch2"
"start:ch2": "yarn build:ch2 && yarn serve:ch2",
"start:ch4": "yarn build:ch4 && yarn serve:ch4"
},
"lint-staged": {
"**/*.js": [
Expand Down

0 comments on commit 445da92

Please sign in to comment.