I'm surprised by the huge variance. Can these results be trusted?
But, browsers are an odd case where optimizations often prove not to be worth it on real world websites. The cost to optimize can easily exceed the savings.
This small sample (2 browsers on macOS) align with my expectations of "Good luck optimizing JavaScript that has dozen of different implementations".
We should not make assertions based on a language name, when there are so many implementations to chose from, https://zoo.js.org/
> All the loops call a dummy function DATA.doSomethingWithValue() for each array element to make sure V8 doesn't optimize out something too much.
This is probably the most worrying comment - what is "too much?" How are you sure it doesn't change between different implementations? Are you sure v8 doesn't do anything you don't expect? If you don't look into what's actually happening in the engine, you have no idea at this point. Either you do the real work and measure that, or do the fake work but verify that the engine does what you think it does.
A bit more explanation of “something” and ”too much“ would have been educational
You probably still don't want to risk it in a tight loop. Relying on those optimizations may not be the most reliable choice when you think about all the different JS engines, platforms, system load, operating systems...
Careful, you are in violation of the GDPR. You can’t simply say “I’m going to set non-essential advertising cookies, deal with it or get out” (I checked your policy to confirm you are using non-essential cookies), you can’t set any cookies until the user agrees, and have to provide a way to reject them that is at least as simple as the way to accept.
Despite what many EU citizens may think, EU laws don't apply to the world.
Why are you even mad? This doesn’t affect you, it is not directed at you, and is trying to be a helpful clarification to the author. If they care about compliance, presumably they care about doing it right.
> Despite what many EU citizens may think, EU laws don't apply to the world.
I’m a EU citizen living in the EU and have never encountered or heard of anyone with that belief.
See 4chan vs OSA.
EU is similarly trying to legislate for the world, and it will work until someone calls on their bluff.
I'm in EU most of the time and like most benefits of GDPR (right to access and delete? Great!) But sometimes they overplay their hand.
OSA is a UK legislation. The UK is not in the EU.
> EU is similarly trying to legislate for the world, and it will work until someone calls on their bluff.
That’s an opinion. The Brussels Effect is real, but that’s not the same as “many EU citizens think EU laws apply to the world”.
And try to check EU GDPR territoriality and tell me whether you're of a different opinion:
https://commission.europa.eu/law/law-topic/data-protection/r...
> The GDPR applies to: > a company or entity which [...]; or > a company established outside the EU and is offering goods/services (paid or for free) or is monitoring the behaviour of individuals in the EU.
Now imagine running a forum or any other service from outside of the EU, on the open web. Does the EU think GDPR applies to you?
In other words, that doesn’t mean “EU laws apply to the world”, it means “EU laws apply to whoever operates in the EU”.
Any source for that?
GDPR only applies to companies having a branch in the EU or offering services targeted at EU citizens: https://commission.europa.eu/law/law-topic/data-protection/r...
> a company established outside the EU and is offering goods/services (paid or for free) or is monitoring the behaviour of individuals in the EU.
If you serve traffic to the EU (like any website on the open web as originally intended) you fall into that.
In the case of blogs like this, they don't really care about compliance, it's just LARPing. "Serious websites have cookie banners, so I'll put a cookie banner too!" It's the same as putting "no copyright infringement intended" into your YouTube video description.
Not true. In fact, those banners are in general way more compliant than before due to the work of organisations like https://noyb.eu/. They have also successfully led numerous initiatives to fine those companies and fix non-compliance.
Usually at this point is when someone comes along to move the goalpost to “but those aren’t very affective”, or “but they still do it”. Sure, argue against malicious compliance and results all you want, I’m simply pointing out that it’s not true at all that “nobody cares”. Enough people care enough that entire organisations exist with that mission.
> In the case of blogs like this, they don't really care about compliance, it's just LARPing.
Maybe. If they don’t care, they can simply ignore my advice. If they do care, they can follow it. Either way, there is no situation in which my sharing of the law is negative.
https://github.com/airbnb/javascript/issues/1122
https://github.com/airbnb/javascript/issues/1271
Because the style guide thinks "forEach is cleaner than for-of". (No I did not make that up.)
https://github.com/airbnb/javascript?tab=readme-ov-file#iter...
https://github.com/airbnb/javascript/issues/1122#issuecommen...
https://github.com/airbnb/javascript/issues/1122#issuecommen...
There were several extremely long and unproductive GitHub discussion with him posted to HN before, where he sent unsolicited and unwanted PRs to lots of projects that tanked performance by orders of magnitude and introduced tens of dependencies (he wrote himself), to ensure that projects would be backwards compatible with completely irrelevant targets they never supported in the first place (like Internet Explorer 1.3 under Windows 95 or something along those lines).
When projects did not want to take these contributions, he played his TC39 authority card, and when that did not help became more and more incoherent in his ramblings.
My take is that this guy actually wants to and thinks he is doing good, while actually having lost the plot a while ago.
i used to be like ljharb, changing company/stack made me live in python for loops but then i miss composable operators ..
Not really, their rational is written in the document I linked --
Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
// bad
let sum = 0;
for (let num of numbers) {
sum += num;
}
sum === 15;
// good
let sum = 0;
numbers.forEach((num) => {
sum += num;
});
sum === 15;
Which is... ridiculous. None of this is actually immutable, as "sum" is constantly being modified. A real FP purist would be using "reduce" in the "good" example. Otherwise, forEach is not better than for-of in terms of readability or maintainability in any way.In addition, I think an issue is that for a long time, when you use ESLint CLI to create a new config file, airbnb is the default option, which ends up making it used very widely, even after the config itself is in maintenance mode. They were only removed in 2024: https://github.com/eslint/create-config/pull/108
I say this as someone who loves JS since before The Good Parts book.
> in the latest version of all browsers. Despite marketing, no browsers are "evergreen" according to the google analytics of major websites I've been able to review over the last couple years. (Nothing but safari will likely ever support PTC - which is not an optimization - so that's not really relevant to discuss) Performance isn't important, readability is.
ref: https://github.com/airbnb/javascript/issues/1122#issuecommen...
> lets everyone move past discussing such details in perpetuity
That's definitely a reasonable attitude towards coding standards generally. However, in this case, there are "better" eslint rules out there that flags real, serious issues but still allows you to be productive, which is what I end up using.
Too bad ESLint has been recommending airbnb as the style until 2024. You can't expect everyone to start with nothing and hand pick every rule themselves. Doable for a large organization, not possible for a personal project.
Even then, far less than an amount one needs to be concerned of, for "performance"
At least for of is better than forEach (ill never forgive crockford for goading people into this functional-lite code with horrible runtime footprint), but these things are fetishes.
This was added to the language 10 years ago. So while it's "newer" than a plain old for-loop, it's definitely not "new". It was designed to work with Symbol.iterator. This is the mechanism whereby one can iterate anything that implements the Symbol.iterator interface.
As far as why folks won't just do simple for-loops, it's the same reasining every language tends to implement a "foreach", because there are annoying off-by-one errors lurking in the < vs <=. Of course one could argue that developers should be smart enough to handle this. But that's an argument even older than for-loops.
These barely if ever happen for simple iterations, it's an idee fixe. Off-by-one sneak in when trying to do something special, and then a for-on loop is useless anyway.
> Symbol.iterator interface
I get it, but it's unnecessary abstraction to overgeneralize a vanilla array to an iterable.
anyway, for..of is not an issue, it's alright. I think if there's one main point i'm trying to make, is that devs will write objectively worse code base on gut wants (in this case, obsession to use higer level abstractions to iterate over an array).
Personally, I like the for-of syntax as well as for-await-of... I kind of wish there was a promise-based version of forEach, as sometimes this syntax sits better, though I'm more inclined to use map, find, etc. over forEach. If I'm not using the indexer value, then there's not much point in using arr[i] over the for-of.
Also, in practice, I've RARELY found the need to optimize how JS is used outside refactoring a recursive algorithm when there's risk of a stack overflow.
Take a look at the https://kepler.gl/ demos to see quite how sophisticated this stuff can get now - millions of geospatial data points rendered entirely clientside via WebGL.
phillipseamore•4d ago
senfiaj•3d ago
phillipseamore•2d ago
senfiaj•2d ago
I created another benchmark https://jsben.ch/sdaEM . The difference is that doSomethingWithValue now assigns the passed value to some variable. I think the optimizer could still notice that the function was dummy and do weird things, but not sure 100%. So I store in a variable to prevent this. Now the tests run slower, but still both classic and for-of tend to be 100%. forEach is sometimes slower, sometimes on par with the other 2 (on both AMD Ryzen 5000U Laptop and Pixel 3a).
Could you please run this test on your mentioned machine? Also could you gradually increase the number of repeats in each test case and see if there is some convergence? Also make sure the tab and the browser window are active during the benchmark. Otherwise the browser / OS might give less priority to the task. And BTW, can you also test on Chrome 143 and even Chrome 144 (it will be released tomorrow)?
jauntywundrkind•1d ago
Side note, I am fantastically glad to see at least some kind of online javascript benchmarking site is alive again. https://jsperf.com was so good to us for so many years. Obviously there's flaws but the way that it not just was a benchmark site but also would record folks results & aggregate them was incredibly incredibly incredibly fantastically sweet.
throw_m239339•1d ago