Friday 26 June 2020

Performance issue in function parameters binding

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.jsOne of the places this binding is required (method: async process):https://github.com/RyanJ93/lala.js/blob/master/lib/Server/processors/HTTP/HTTPRequestProcessor.jsAnd 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.

Submitted June 26, 2020 at 11:45PM by RyanJ93

No comments:

Post a Comment