Skip to content

Instantly share code, notes, and snippets.

@oklai
Last active December 20, 2015 04:09
Show Gist options
  • Save oklai/6069113 to your computer and use it in GitHub Desktop.
Save oklai/6069113 to your computer and use it in GitHub Desktop.
The performance test between use module.exports and Function.prototype in node.

Here my results

test1()
Time: 10578 ms
Memory: 30912 K

test2()
Time: test2: 10798 ms
Memory: 30956 K

Appears to use in module.exports will be quicker

var useExports = require('./useExports'),
usePrototype = require('./usePrototype.js');
var test1 = function () {
console.time('test1');
for (var i = 0; i < 1000000; i++) {
useExports.run();
}
console.timeEnd('test1');
}
var test2 = function () {
console.time('test2');
for (var i = 0; i < 1000000; i++) {
usePrototype.run();
}
console.timeEnd('test2');
}
// Run these two cases respectively
// test1()
// test2()
exports.run = function () {
var test = {}
for (var i = 0; i < 1000; i++) {
test[i] = i;
}
return test;
}
function Test () {
}
module.exports = new Test();
Test.prototype.run = function() {
var test = {}
for (var i = 0; i < 1000; i++) {
test[i] = i;
}
return test;
};
@oklai
Copy link
Author

oklai commented Jul 24, 2013

Hi @zaygraveyard,
I create a demo for test the performance test between use module.exports and Function.prototype in node. You can also test and tell me the result.
If my test method is not correct, please correct me

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment