-
Notifications
You must be signed in to change notification settings - Fork 10
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
Browser tests #2
Comments
Here are the test results on a different hardware. Chrome 121.0.6167.86 on Windows:
Node v20.10.0 on WSL2 Ubuntu:
The results vary, but loops are still faster on average. |
My initial thought was about objects, I retested it using object once more. I used this setup function buildReduceSuite(array) {
return new Benchmark.Suite("reduce")
.add("Array.reduce", function () {
return array.reduce((p, x, i) => {
p[i] = x.a + x.b;
return p;
}, {});
})
.add("Array.reduce (spread)", function () {
return array.reduce((p, x) => ({ ...p, [x]: x.a + x.b }), {});
})
// .add("for of (indexOf)", function () {
// const result = {};
// for (let x of array) {
// const i = array.indexOf(x);
// result[i] = x.a + x.b;
// }
// return result;
// })
.add("for of (entries)", function () {
const result = {};
for (let [i, x] of array.entries()) {
result[i] = x.a + x.b;
}
return result;
})
.add("for", function () {
const result = {};
for (let i = 0; i < array.length; ++i) {
result[i] = array[i].a + array[i].b;
}
return result;
});
} I got these results with node 21
I see that plain My conclusions:
Please update your |
Well, objects add a lot of overhead to the tests. That's why there's only a minor difference. In production code, it is best to use whatever makes it simple and more readable, focusing on performance only when it matters. For example, when dealing with large arrays and simple operations. With your setup, I've gotten results similar to yours. However, when changing the code to a simple sum, removing quite heavy object construction:
I've got these results:
We'll update the Readme and the article with Node v20 numbers, as it's not four times slower anymore. |
@Andriy-LL Nice to hear about upcoming changes, but my point about objects were exactly about objects. It also goes along with convenience and readability since it's good to know that you can make it fast and readable without pain and In details, what I meant in my examples is when you use spread syntax in Anyway, I use |
Please, provide tests for browsers, at least Chrome.
I decided to recreate the tests myself and I found out that results are different for browsers.
It seems that
reduce
has either the same performance asfor..of
or even better!Also, please redo tests for Node 20LTS, which is latest, node recently got optimization updates, maybe you will get similar results to browsers environment.
The text was updated successfully, but these errors were encountered: