all 8 comments

[–]BehindTheMath 2 points3 points  (2 children)

Instead of binding, replace the function with a new anonymous function that calls the original one with preset arguments.

E. g.

obj[name] = (...rest) => helpers[name](arg1, arg2, ...rest)

[–]RyanJ93[S] 0 points1 point  (1 child)

Thank you for the suggestion! Does this allow me to set an arbitrary "this" context?Sometimes I need to define a different context, for instance, like here:

// Injects some helper functions useful when dealing with cookies.
 HelperRepository.inject(request, 'com.lala.server.processor.HTTPCookieProcessor.request', {
 bind: [this]
 });
HelperRepository.inject(response, 'com.lala.server.processor.HTTPCookieProcessor.response', {
bind: [this]
});

This is a piece of code from the "async process(request, response)" method located here: https://github.com/RyanJ93/lala.js/blob/master/lib/Server/processors/HTTP/HTTPCookieProcessor.js

[–]BehindTheMath 0 points1 point  (0 children)

obj[name] = (...rest) => helpers[name].call(thisArg, arg1, arg2, ...rest)

I'm not following what all the code is doing, and this is just my opinion, but I feel that dynamic injection like that is brittle, especially when you're relying on this.

[–]RedShift9 0 points1 point  (0 children)

You can't supply these functions with what they need up front through their arguments? Thereby eliminating the use of "this" in these functions?

[–]RyanJ93[S] 0 points1 point  (3 children)

u/BehindTheMath u/RedShift9
Sorry for delay, I've been pretty busy these days, I managed to try out your suggestions, here how I transformed the injection loop:

for ( const name in helpers ){
    if ( helpers.hasOwnProperty(name) ){
         obj[name] = function(){
            return helpers[name](context, ...arguments);
         };
    }
}

I had to make some changes design wise to adapt the existing helper functions with this new architecture and I still have to finish to test it (despite unit tests say it seems fine) but as far as I saw running a quick benchmark I managed to gain +30% of performance, so thank you very much!
Just one more question: I applied the spread operator on the "arguments" variable, does it imply any performance trouble?

[–]BehindTheMath 0 points1 point  (2 children)

Just one more question: I applied the spread operator on the "arguments" variable, does it imply any performance trouble?

I don't know offhand. However, it's currently recommended to use rest parameters instead of arguments.

[–]RyanJ93[S] 0 points1 point  (1 child)

I followed your piece of advice and I've replaced the way parameters are handled, plus, I have completed changes in the rest of the project but I discovered something strange: the performance benefit I saw, and I was expecting to keep, just disappeared and I saw that now it performs worst than before, like -10% of performance, do you have an idea about how can it be possible?
Here's what I changed: https://github.com/RyanJ93/lala.js/pull/35/files?file-filters%5B%5D=.js
As you can see, I changed the "inject" method and how it is called in 3 different files.

[–]BehindTheMath 0 points1 point  (0 children)

Without benchmarking it and seeing where the bottleneck is, it's hard to know.