Optimize lang.bind
I figured this would be a low hanging fruit as far as making the JS code more efficient because it's one of the most used functions in GNOME Shell and Cinnamon. This refactoring was driven by benchmarking various iterations of the bind function with jsben.ch (original link, see comment) using the Firefox 60 ESR.
The least efficient method was using .call()
instead of .apply()
with an arguments spread. Normally .call
is a little faster, but it looks like ...spreading
isn't as efficient when passed as a function argument. This was a different story for the spread operator being used as a parameter though, compared to slicing arguments, which was faster than using Array.prototype.slice
.
This also removes error handling. I'm not sure this is something that is wanted, but I think there is a case to be made for its removal. The main thing is if the code throws, I think it's better if developers see what error the JS engine is throwing. Recognizing native error messages over time makes people better JS developers, in my opinion. The code is going to throw in either case with a stack trace, so there's no loss of safety as far as I can tell.
Just for fun, this is Chrome 72:
Edit: I didn't actually check GS before opening this, apparently this is dead code as far as it's concerned, though it might still be useful for extensions.