Skip to content

Instantly share code, notes, and snippets.

@JacksonTian
Last active August 29, 2015 14:06
Show Gist options
  • Save JacksonTian/11e185edf1e0ec14c369 to your computer and use it in GitHub Desktop.
Save JacksonTian/11e185edf1e0ec14c369 to your computer and use it in GitHub Desktop.
call_vs_apply

script:

$ cat server.js 
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200);
  res.write('hello world!');
  res.end();
}).listen(1334);

before:

$ ~/git/node/tools/wrk/wrk -d30 http://localhost:1334/
Running 30s test @ http://localhost:1334/
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.04ms  472.50us   7.74ms   87.76%
    Req/Sec     5.18k     0.90k    7.11k    65.22%
  293083 requests in 30.00s, 36.34MB read
Requests/sec:   9769.57
Transfer/sec:      1.21MB

after:

$ ~/git/node/tools/wrk/wrk -d30 http://localhost:1334/
Running 30s test @ http://localhost:1334/
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.00ms  461.37us  10.04ms   87.28%
    Req/Sec     5.34k     0.99k    7.67k    65.03%
  303840 requests in 29.99s, 37.67MB read
Requests/sec:  10129.74
Transfer/sec:      1.26MB
var method = function (a, b, c) {
return a + b + c;
};
var call_method = function (a, b, c) {
method.call(null, a, b, c);
};
var apply_method = function (a, b, c) {
method.apply(null, arguments);
};
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
// add tests
suite.add('call_method()', function () {
call_method(1, 1, 1);
})
.add('apply_method()', function () {
apply_method(1, 1, 1);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run({ 'async': true });
// $ node apply_call.js
// call_method() x 34,868,774 ops/sec ±0.98% (90 runs sampled)
// apply_method() x 25,108,073 ops/sec ±1.38% (86 runs sampled)
// Fastest is call_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment