Core_dumped
Core_dumped

Reputation: 1611

nodejs: Is calling functions from require's cache as fast as functions in global scope?

Suppose I have a file called external.js:

// external.js

//define a print function and make it public
module.exports.print = function(text) {
    console.log(text)
}

And then I have a file called main.js:

// main.js

// call require('./external.js').print so that it'll be in the require's cache
require('./external.js').print('I am now in the cache')

// and define a function equivalent to print in the global scope
function localPrint(text) {
    console.log(text)
}

// Finally, define two functions which use the localPrint and the print in external.js
function echo1(text) {
    require('./external.js').print(text)
}

function echo2(text) {
    localPrint(text)
}

Will there be any difference in performance between echo1 and echo2?
I dare say there will be not. Accessing a global function should be as fast as a function in the require's cache. What do you say?

Upvotes: 0

Views: 127

Answers (2)

Alexis Paques
Alexis Paques

Reputation: 1975

I just did the benchmark :

main.js

// main.js

// call require('./external.js').print so that it'll be in the require's cache
var e = require('./external.js');

// and define a function equivalent to print in the global scope
function localPrint() {
    var hello = "hello";
    return hello;
}

var f = {
  hello : function(){
    var hello = "hello";
    return hello;
  }
}

// Finally, define two functions which use the localPrint and the print in external.js
function echo1() {
    e.hello();
}


function echo2() {
    localPrint()
}

function echo3(){
  f.hello();
}

(function (){
  var times = 100000000;
  var start1 = +new Date();
  for (var i = 0; i < times; i++) {
    echo1('text');
  };
  var stop1 = +new Date();

  var start2 = +new Date();
  for (var i = 0; i < times; i++) {
    echo2('text');
  };
  var stop2 = +new Date();

  var start3 = +new Date();
  for (var i = 0; i < times; i++) {
    echo3('text');
  };
  var stop3 = +new Date();

  console.log('From require', stop1,start1, stop1 - start1);
  console.log('From local', stop2,start2, stop2 - start2);
  console.log('From object', stop3,start3, stop3 - start3);
})();

external.js

// external.js
//define a print function and make it public
module.exports.hello = function() {
    var hello = "hello";
    return hello;
}

result :

From require 1430050276387 1430050276313 74
From local 1430050276460 1430050276387 73
From object 1430050277569 1430050276460 1109

The difference for 100M calls each is not significant from the require or for a local variable (73 & 74 ms). BUT, from an object is 15 times slower (1109 ms) than using a require...

Use the powerful module system of NodeJS instead of creating big objects.

Hope it helps & I hope it is correct!

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074385

Will there be any difference in performance between echo1 and echo2?

Perhaps a trivially small one, yes. echo1 makes an unnecessary function call (at least one, require probably makes several others) and an unnecessary property lookup (on the object returned) (and again, at least one; require probably has to do a couple of property lookups to find your resource in cache).

Whether it matters is another question entirely.

I'd probably do this:

var print = require('./external.js').print;

Or if you really prefer another name:

var echo = require('./external.js').print;

Or if there's a reason for wrapping the call:

var print = require('./external.js').print;
function echo(text) {
    print(text);
}

Upvotes: 1

Related Questions