Skip to content

Instantly share code, notes, and snippets.

@bertolo1988
Last active November 25, 2016 19:19
Show Gist options
  • Save bertolo1988/78d07aa78b3679635c7ab9368f8f4013 to your computer and use it in GitHub Desktop.
Save bertolo1988/78d07aa78b3679635c7ab9368f8f4013 to your computer and use it in GitHub Desktop.
Do functions decrease performance? Answer: No. Notice the extra let obj=[].
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