Skip to content

Instantly share code, notes, and snippets.

@mcollina
Created July 28, 2017 22:51
Show Gist options
  • Save mcollina/40e91ad55ae9c3b0131c2a8b79387dfe to your computer and use it in GitHub Desktop.
Save mcollina/40e91ad55ae9c3b0131c2a8b79387dfe to your computer and use it in GitHub Desktop.
'use strict'
var benchmark = require('benchmark')
var suite = new benchmark.Suite()
var runs = 0
class MyClass {
constructor (x) {
this.x = x
}
}
function MyCtor (x) {
this.x = x
}
var res = 0
function doSomething (obj) {
res = obj.x
}
suite.add('literal', function base () {
var obj = { x: 1 }
doSomething(obj)
})
suite.add('class', function allNums () {
var obj = new MyClass(1)
doSomething(obj)
})
suite.add('constructor', function allNums () {
var obj = new MyCtor(1)
doSomething(obj)
})
var literal = { x: 1 }
suite.add('create', function allNums () {
var obj = Object.create(literal)
doSomething(obj)
})
suite.on('cycle', () => runs = 0)
suite.on('complete', require('./print'))
suite.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment