Arrow function vs thisArg: an Array.prototype.forEach benchmark

Posted on

Here’s a 5-min script that checks the performance different ways of accessing this from within nested functions.

The question stems from an application design (this one’s a Lambda Node.js app) that heavily uses bound contexts – within array.prototype.forEach loops I needed to call functions from the bound context.

Googling yielded naught, so… benchmark time.

Perhaps had I spent a minute more thinking about it – I would have realised that the question is trivial because the reference to the context is bound once in both cases… so neither should technically have the performance benefit

And indeed the numbers showed an average 25ms execution for both test. Lel. (i7-3770, Node.js v7.2.1)

Oh well – perhaps a useful test harness for future usage.

Usage (assuming your .js file is called arrowvsthisarg.js):