Observed in the front-end, it's usual practice to use forEach loops in places where there's no need to iterate over the whole array or set, but only till finding the first element that matches a condition. We can probably do a much better job at having a more performant front-end logic.
Description
Description
Details
Details
Subject | Repo | Branch | Lines +/- | |
---|---|---|---|---|
Fix wrong usage of forEach where looping over the whole set has no sense | mediawiki/extensions/WikiLambda | master | +93 -149 |
Event Timeline
Comment Actions
I see these possibilities :
- Either we can use Array.prototype.find because it is supported by all modern browsers and we do not support IE11
- Otherwise:
- we can probably use Array.prototype.some just like we currently use Array.prototype.forEach and return true when we want to stop iterating and return`false` otherwise
- a good old for (...;...;...) loop with a break statement
- Use some Array.prototype.find polyfill (core-js or even babel which uses it)
(and there might be other possibilities :) )
Resources:
- Array.prototype.find documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
- Array.prototype.find compatibility in browsers: https://caniuse.com/array-find
- Array.prototype.some documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
Comment Actions
Change 865052 had a related patch set uploaded (by Teleosteen; author: Teleosteen):
[mediawiki/extensions/WikiLambda@master] Fix wrong usage of forEach where looping over the whole set has no sense
Comment Actions
Change 865052 merged by jenkins-bot:
[mediawiki/extensions/WikiLambda@master] Fix wrong usage of forEach where looping over the whole set has no sense