Tuesday, 26 November 2019

Node.js performance issue?

I was making some benchmarks on Node.js and I found something interesting: a potential performance issue.Try running this piece of code with Node (do not try to run it on the browser since it seems there are no problems with javascript on the browser):function benchmark(nrOfTimesToRun, fn) { const startTime = +new Date(); for (let i = 0; i < nrOfTimesToRun; i++) { fn(); } return +new Date - startTime; } class Property{ static get(target) { return target[this.KEY]; } } class NameProperty extends Property{} NameProperty.KEY = 'name'; class RoleProperty extends Property{} RoleProperty.KEY = 'role'; class User {} var user = new User NameProperty.get(user); // RoleProperty.get(user); const result = benchmark(1e7, () => { NameProperty.get(user); }); console.log('Execution time: %d ms', result); In my case, Node completed the task in `77 ms`. Now uncomment the `RoleProperty.get(user);` line and run the code again.BOOM. My pc finished the task in `1162 ms`.​What is the problem here? Why it is taking so slow to run the benchmark in the second case?​Debugging this I found that `return target[this.KEY];` might be the problem. More exactly `this.KEY`. I think Node has a hard time getting the static property.​Is this a known problem of Node?

Submitted November 26, 2019 at 02:31PM by IonelLupu

No comments:

Post a Comment