Created
November 21, 2012 00:03
-
-
Save drewfish/4122136 to your computer and use it in GitHub Desktop.
cost of conditional function call
This file contains hidden or 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
var microtime = require('microtime'), | |
i, iterations = 1000000, | |
t0, t1, base, | |
runFoo, | |
whatever = 1000000000000000, | |
obj = { | |
foo: function () { | |
whatever += 1; | |
}, | |
run: function () { | |
whatever += 1; | |
} | |
}; | |
function report(time, name) { | |
//console.log(time + '\t+' + Math.round(100*(time-base)/base) + '%\t' + name); | |
console.log(time + '\t' + Math.round(100*time/base) + '%\t' + name); | |
} | |
function switchFoo(bool) { | |
runFoo = bool; | |
} | |
// baseline | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
base = t1 - t0; | |
console.log('-------------------- baseline'); | |
report(base, 'run()'); | |
console.log(); | |
console.log('-------------------- have foo'); | |
switchFoo(true); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
if (runFoo) obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'if bool, foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
if (obj.foo) obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'if foo, foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
runFoo && obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'bool && foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
obj.foo && obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'foo && foo()'); | |
console.log(); | |
console.log('-------------------- no foo'); | |
switchFoo(false); | |
delete obj.foo; | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
if (runFoo) obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'if bool, foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
if (obj.foo) obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'if foo, foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
runFoo && obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'bool && foo()'); | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
obj.foo && obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'foo && foo()'); | |
console.log(); | |
console.log('-------------------- no-op foo'); | |
obj.foo = function() {}; | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
if (obj.foo) obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'if foo, foo()'); | |
runFoo = true; | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
obj.foo && obj.foo(i); | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'foo && foo()'); | |
console.log() | |
console.log('-------------------- misc'); | |
obj._run = obj.run; | |
obj.run = function () { | |
this._run(); | |
}; | |
t0 = microtime.now(); | |
for (i = 0; i < iterations; i += 1) { | |
obj.run(); | |
} | |
t1 = microtime.now(); | |
report(t1 - t0, 'wrapped run()'); | |
console.log('whatever =', whatever); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment