And, bringing this back to the original poster's request for callback- taking API functions which take "this" arguments, I compared simulating the two methods in the last post, as well as if the API functions allowed for a "this" to be passed in [1], and heap growth per call was roughly:
closure: 200 bytes .bind: 620 bytes passing "this": 104 bytes So, at least heap pressure could be reduced if, in general, we never needed to do .bind or create a closure (though for actual async functions, the overhead of actually doing things like file I/O far outweigh a few hundred bytes, and for EventEmitters it sounds like there are reasonable ways around it anyway). [1] https://gist.github.com/2899468 On Jun 8, 9:21 pm, Jimb Esser <[email protected]> wrote: > By "binding more than once", I mean binding the same original function > multiple times (with different parameters), not binding the result of > a previously bound function. > > An example of using bind instead of a closure: > original: > function backupFile(filename, cb) { > fs.unlink(filename + '.bak', function(err) { > fs.rename(filename, filename + '.bak', cb); > });} > > Using bind and no run-time function creation: > function backupFile(filename, cb) { > fs.unlink(filename + '.bak', step2.bind(undefined, filename, cb));} > > function step2(filename, cb, err) { > fs.rename(filename, filename + '.bak', cb); > > } > > Doing some microbenchmarks, it does seem, however, that the .bind > version is allocating more memory (only using a little more on older > V8 versions, but seems to be using over triple the amount of the > closure on the V8 version in node 0.6.18), so I retract my earlier > statement about bind generally being better performing. I was > confusing the results of my anecdotal evidence, which was that > implementing your own .bind function (or accidentally including a > library that overrides Function.prototype.bind) is notably slower, but > V8 seems good at optimizing closures in a local scope. > > In theory the closure can also grab references to local variables in > the scope, causing them not to get garbage collected as quickly, but > in practice V8 seems to not grab references to the entire scope, just > the appropriate objects. > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
