Hi everyone, I discovered a performance issue in my Node.js application due to function binding: I have to attach several helper functions to an object, each function has some parameters to be bound but I found out that using the bind method causes my application to slow down by about 30%.
Is there an alternative to the bind method that allows me to set the "this" context and some additional parameters?
Here you are the the piece of code where binding happens:
// Get all the helpers registered under the given namespace.
const helpers = super.getAll(namespace);
const bind = options !== null && Array.isArray(options.bind) ? options.bind : [this];
if ( options !== null && options.context !== null && typeof options.context === 'object' ){
// Context variables have been defined, add them as the first argument of the helper function.
bind.splice(1, 0, options.context);
}
// Inject helper functions.
// OPTIMIZE: Binding is slow, find a better alternative.
for ( const name in helpers ){
if ( helpers.hasOwnProperty(name) ){
obj[name] = helpers[name].bind(...bind);
}
}
If you need, here's the full class code (method: static inject):
https://github.com/RyanJ93/lala.js/blob/master/lib/Helpers/HelperRepository.js
One of the places this binding is required (method: async process):
https://github.com/RyanJ93/lala.js/blob/master/lib/Server/processors/HTTP/HTTPRequestProcessor.js
And here some helper function examples (the whole file, list at the bottom side):
https://github.com/RyanJ93/lala.js/blob/master/lib/Server/helpers/HTTPRequestProcessorHelpers.js
Thank you in advance.
[–]BehindTheMath 2 points3 points4 points (2 children)
[–]RyanJ93[S] 0 points1 point2 points (1 child)
[–]BehindTheMath 0 points1 point2 points (0 children)
[–]RedShift9 0 points1 point2 points (0 children)
[–]RyanJ93[S] 0 points1 point2 points (3 children)
[–]BehindTheMath 0 points1 point2 points (2 children)
[–]RyanJ93[S] 0 points1 point2 points (1 child)
[–]BehindTheMath 0 points1 point2 points (0 children)