-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is the new Function performance really good? #162
Comments
Try to call the tap many many times |
First of all thank you for your reply. I try to call the callback 10,000 times, and the result is that the official tapable synchook execution time is still dozens of times longer than I wrote. Does this mean that the method of dynamically generating the function body through new Function is not suitable? Finally, here are some tapable hooks I wrote myself. I tested that the execution time of these hooks is shorter than the official tapable hooks. Of course, the robustness is not as strong as the tapable official. mini-tapable |
@sokra I just called the tap 10,000 times,and found that the official tapable sync hook took almost 60ms, while my synchook took |
Try to call |
@sokra This time I call the tap 1000 times and call the tap(plugin, callback){ call(...args){ const compilation = { sum: 0 } for(let i = 0; i < 1000; i++){ myHook.tap( console.time('tapable') console.time('my') ` Thank you! |
@sokra I think it should be the new version of nodejs that has been optimized, and the performance is better than the function body dynamically generated by new Function. |
`const { SyncHook } = require('tapable') tap(plugin, callback){ const compilation = { sum: 0 } for(let i = 0; i < 1000; i++){ myHook.tap("plugin" + i, (compilation) => { console.time('tapable') console.time('my') This time i register 1000 plugins and call the |
One major difference it that you pay the compilation cost only on the first compilation and each following compilation is fast after that. tapable also optimizes for the polymorphic case, so when different plugins are added and multiple hooks are used. The But I played around with benchmarking the different approaches with the following conclusions:
=> While tapable is a bit slower when Hooks are small or called only a few times, it's very faster when hooks are called often. But when Hooks are small or called only a few times, they are also very fast in general, so maybe that's not the thing we should focus on. Anyway maybe it makes sense to have a "interpreter mode" which is used for the first 10 calls, so we improve performance on hooks that are only calls once. |
wow, you are so nice and patient. Thank you! My last question is, why not reset this.call with CALL_DELEGATE , but use this._call. such as |
JavaScript 引擎的编译优化: new Function 方法: 当你使用 new Function 创建一个函数时,这个函数通常会被 JavaScript 引擎视为一个独立的脚本。这意味着它可以进行更彻底的优化。由于该函数是在运行时创建的,引擎有机会根据当前上下文优化它,这可以包括内联优化、避免不必要的变量查找等。 在 forEach 循环中,每次迭代都会进行一次函数调用。这些调用涉及到创建调用上下文、传递参数、以及在调用栈上进出的开销。 |
When comparing the use of new Function to create and execute functions with directly using a forEach loop to execute functions, the performance differences primarily stem from two aspects: the compilation optimization of the JavaScript engine and the overhead of function calls. JavaScript Engine's Compilation Optimization: Using new Function Method: When you create a function using new Function, it is often treated by the JavaScript engine as an independent script. This means it can undergo more thorough optimization. As the function is created at runtime, the engine has the opportunity to optimize it based on the current context, which can include inline optimization, avoiding unnecessary variable lookups, etc. In a forEach loop, each iteration involves a function call. These calls involve creating a calling context, passing parameters, and the overhead of entering and exiting the call stack. |
I simply implemented a MySyncHook, and then traversed the callback function directly in the call method, and printed the execution time. At the same time, I used tapable's official SyncHook to execute the same logic, and found that the execution time of tapable's SyncHook was dozens of times longer than my own. Could anyone please help me answer.
The text was updated successfully, but these errors were encountered: