Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function#bind() is often slow. #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function bind(fn, context) {
return function __bind__ () {
return fn.apply(context, arguments)
}
}
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var http = require('http')
, Serializer = require('./serializer')
, Deserializer = require('./deserializer')
, Cookies = require('./cookies')
, bind = require('./bind')

/**
* Creates a Client object for making XML-RPC method calls.
Expand Down Expand Up @@ -103,7 +104,7 @@ Client.prototype.methodCall = function methodCall(method, params, callback) {

options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8')
this.headersProcessors.composeRequest(options.headers)
var request = transport.request(options, function(response) {
var request = transport.request(options, bind(function(response) {

var body = []
response.on('data', function (chunk) { body.push(chunk) })
Expand All @@ -130,7 +131,7 @@ Client.prototype.methodCall = function methodCall(method, params, callback) {
callback(err, result)
})
}
}.bind(this))
}, this))

request.on('error', callback)
request.write(xml, 'utf8')
Expand Down
14 changes: 8 additions & 6 deletions lib/cookies.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var bind = require('./bind')

/**
* Creates object for cookies manipulation on client side.
* Allows to parse server's response in order to get cookies and compose http request to transfer cookies to the server
Expand Down Expand Up @@ -66,7 +68,7 @@ Cookies.prototype = {
parseResponse: function(headers) {
var cookies = headers['set-cookie']
if (cookies) {
cookies.forEach(function(c) {
cookies.forEach(bind(function(c) {
var cookiesParams = c.split(';')
var cookiePair = cookiesParams.shift().split('=')
var options = {}
Expand All @@ -78,7 +80,7 @@ Cookies.prototype = {
}
})
this.set(cookiePair[0].trim(), cookiePair[1].trim(), options)
}.bind(this))
}, this))
}
},

Expand All @@ -101,10 +103,10 @@ Cookies.prototype = {
*/
toString: function() {
return Object.keys(this.cookies)
.filter(this.checkNotExpired.bind(this))
.map(function(name) {
return name + '=' + this.cookies[name].value
}.bind(this)).join(';')
.filter(bind(this.checkNotExpired, this))
.map(bind(function(name) {
return name + '=' + this.cookies[name].value
}, this)).join(';')
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/deserializer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var sax = require('sax')
, dateFormatter = require('./date_formatter')
, bind = require('./bind')

var Deserializer = function(encoding) {
this.type = null
Expand All @@ -14,12 +15,12 @@ var Deserializer = function(encoding) {
this.error = null

this.parser = sax.createStream()
this.parser.on('opentag', this.onOpentag.bind(this))
this.parser.on('closetag', this.onClosetag.bind(this))
this.parser.on('text', this.onText.bind(this))
this.parser.on('cdata', this.onCDATA.bind(this))
this.parser.on('end', this.onDone.bind(this))
this.parser.on('error', this.onError.bind(this))
this.parser.on('opentag', bind(this.onOpentag, this))
this.parser.on('closetag', bind(this.onClosetag, this))
this.parser.on('text', bind(this.onText, this))
this.parser.on('cdata', bind(this.onCDATA, this))
this.parser.on('end', bind(this.onDone, this))
this.parser.on('error', bind(this.onError, this))
}

Deserializer.prototype.deserializeMethodResponse = function(stream, callback) {
Expand All @@ -44,7 +45,7 @@ Deserializer.prototype.deserializeMethodResponse = function(stream, callback) {
}

stream.setEncoding(this.encoding)
stream.on('error', this.onError.bind(this))
stream.on('error', bind(this.onError, this))
stream.pipe(this.parser)
}

Expand All @@ -67,7 +68,7 @@ Deserializer.prototype.deserializeMethodCall = function(stream, callback) {
}

stream.setEncoding(this.encoding)
stream.on('error', this.onError.bind(this))
stream.on('error', bind(this.onError, this))
stream.pipe(this.parser)
}

Expand Down
9 changes: 5 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var http = require('http')
, EventEmitter = require('events').EventEmitter
, Serializer = require('./serializer')
, Deserializer = require('./deserializer')
, bind = require('./bind')

/**
* Creates a new Server object. Also creates an HTTP server to start listening
Expand Down Expand Up @@ -62,13 +63,13 @@ function Server(options, isSecure, onListening) {
this.httpServer = isSecure ? https.createServer(options, handleMethodCall)
: http.createServer(handleMethodCall)

process.nextTick(function() {
process.nextTick(bind(function() {
this.httpServer.listen(options.port, options.host, onListening)
}.bind(this))
this.close = function(callback) {
}, this))
this.close = bind(function(callback) {
this.httpServer.once('close', callback)
this.httpServer.close()
}.bind(this)
}, this)
}

// Inherit from EventEmitter to emit and listen
Expand Down