Last active
November 25, 2016 19:19
-
-
Save bertolo1988/78d07aa78b3679635c7ab9368f8f4013 to your computer and use it in GitHub Desktop.
Do functions decrease performance? Answer: No. Notice the extra let obj=[].
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 debug = require('debug')('test'); | |
function sum(a, b) { | |
return a + b; | |
} | |
function multiplyTwo(a) { | |
return a * 2; | |
} | |
function divideByTwo(a) { | |
return a / 2; | |
} | |
function subtractOne(a) { | |
return a - 1; | |
} | |
function runOne(a, times) { | |
let obj = []; | |
//with functions | |
for (let i = 0; i < times; i++) { | |
a = sum(a, 1); | |
a = multiplyTwo(a); | |
a = divideByTwo(a); | |
a = subtractOne(a); | |
} | |
debug('end with functions ' + a); | |
} | |
function runTwo(a, times) { | |
let obj = []; | |
//without functions | |
for (let i = 0; i < times; i++) { | |
a = a + 1; | |
a = a * 2; | |
a = a / 2; | |
a = a - 1; | |
} | |
debug('end without functions ' + a); | |
} | |
const times = 900000000; | |
debug('start with functions'); | |
runOne(2, times); | |
debug('start without functions'); | |
runTwo(2, times); | |
/* | |
results: | |
/small$ DEBUG=* node a.js | |
test start with functions +0ms | |
test end with functions 2 +10s | |
test start without functions +3ms | |
test end without functions 2 +10s | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment