Skip to content

Instantly share code, notes, and snippets.

@trusktr
Last active August 26, 2018 23:40
Show Gist options
  • Save trusktr/e710fc7d1bc24b13d6d5bd1ae20eef69 to your computer and use it in GitHub Desktop.
Save trusktr/e710fc7d1bc24b13d6d5bd1ae20eef69 to your computer and use it in GitHub Desktop.
Perf tests
function performanceTests(...testCases) {
const NUM_ITERATIONS = 5000000
let results = []
console.log(' -- Running tests... ')
for (let i=0, l=testCases.length; i<l; i+=1) {
results.push(testCases[i](NUM_ITERATIONS))
}
let slowest = results[0]
let fastest = results[0]
for (let result of results) {
if (result.averageTime > slowest.averageTime)
slowest = result
if (result.averageTime < fastest.averageTime)
fastest = result
}
console.log(' -- Done running tests. ')
console.log(` -- slowest: "${slowest.name}" -- `)
console.log(` - total time: ${slowest.totalTime}`)
console.log(` - average time: ${slowest.averageTime}`)
console.log(` -- fastest: "${fastest.name}" -- `)
console.log(` - total time: ${fastest.totalTime}`)
console.log(` - average time: ${fastest.averageTime}`)
}
//function testCase(name, setup, preTest, test, postTest, teardown) {
function testCase(name, {setup, preTest, test, postTest, teardown}) {
return function(iterations) {
if (!test) throw new Error('Um, you need a test.')
const o = {}
let totalTime = 0
let averageTime
let startTime
let endTime
console.log(` -- Test "${name}" -- `)
if (setup) setup(o)
for (let i=0; i<iterations; i+=1) {
if (preTest) preTest(o)
startTime = performance.now()
test(o)
endTime = performance.now()
totalTime += endTime - startTime
if (postTest) postTest(o)
}
if (teardown) teardown(o)
averageTime = totalTime/iterations
console.log(` - total time: ${totalTime}`)
console.log(` - average time: ${averageTime}`)
return {name, totalTime, averageTime}
}
}
performanceTests(
testCase('with .has()', {
setup(o) {
o.s = new Set
o.obj = {}
o.s.add(o.obj)
},
test(o) {
if (!o.s.has(o.obj))
o.s.add(o.obj)
},
}),
testCase('without .has()', {
setup(o) {
o.s = new Set
o.obj = {}
o.s.add(o.obj)
},
test(o) {
o.s.add(o.obj)
},
})
)
performanceTests(
testCase('with check', {
setup(o) {
o.f = window.f
o.b = window.b
},
test(o) {
if (o.f === o.b.parentNode) return
o.f.appendChild(o.b)
},
}),
testCase('without check', {
setup(o) {
o.f = window.f
o.b = window.b
},
test(o) {
o.f.appendChild(o.b)
},
})
)
performanceTests(
testCase('with check', {
setup() {
document.body.style.display = 'none'
},
test() {
if (document.body.style.display == 'none')
document.body.style.display = 'block'
},
}),
testCase('without check', {
setup() {
document.body.style.display = 'none'
},
test() {
document.body.style.display = 'block'
},
})
)
performanceTests(
testCase('with check', {
setup() {
document.body.style.display = 'none'
},
test() {
if (document.body.style.display == 'none')
document.body.style.display = 'block'
},
}),
testCase('without check', {
setup() {
document.body.style.display = 'none'
},
test() {
document.body.style.display = 'block'
},
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment