Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Some code uses $.each, some uses Array.prototype.forEach #1098

Closed
rwaldron opened this issue Jun 21, 2012 · 10 comments · Fixed by #3087
Closed

Some code uses $.each, some uses Array.prototype.forEach #1098

rwaldron opened this issue Jun 21, 2012 · 10 comments · Fixed by #3087

Comments

@rwaldron
Copy link

Much like the Array.isArray in #1096

If this is meant to be used with modern browsers only, then I would expect Array.prototype.forEach to be preferred.

@jasonsanjose
Copy link
Member

Most instances of $.each should be limited to iterating over objects instead of arrays. That said, it's probably worth auditing.

@rwaldron
Copy link
Author

Cool, that makes good sense. I suppose anyone that comes across this ticket may also consider:

var obj = { a: "alpha", b: "beta" };


Object.keys( obj ).forEach(function( key ) {

  // since the |thisArg| was used, |this| is `obj`
  console.log( this[ key ] );

}, obj);


// alpha
// beta

@njx
Copy link
Contributor

njx commented Jun 21, 2012

Hmmm, according to this jsPerf Array.prototype.forEach() is actually a lot slower than just a good old for loop--maybe because it's calling a function for each iteration. Maybe we should just switch to for loops. http://jsperf.com/for-vs-foreach/5

@rwaldron
Copy link
Author

It's true, the bottle neck is at 2 major points:

  1. The stack (ie. execution of the callbackFn)
  2. Sparse array support that checks for "holes" (eg. [ 1, 2, 4, , , , ]) by effectively using "k in array"
[ 1, 2, , 4, , , , ].forEach(function(val) { console.log(val); })

// 1
// 2
// 4

The downside to for-in and for-loop is that you have to manage the scope yourself

@peterflynn
Copy link
Member

@njx: I'm all for that (har!) for very hot performance-sensitive code, but we should be wary of premature optimization elsewhere. All the flavors of loops are very fast, and the loop overhead will almost always pale in comparison to the cycles eaten up by whatever the body of the loop is doing.

I ran some tests on this way back when we were originally talking over coding style. I had a simple test of walking a 10,000-item array 100 times in a row (with a loop body that was barely more than a no-op). In that test, forEach() was 120 ms slower than a vanilla for loop -- hardly nothing, but if we have any code that expects to do 1 million iterations fast I hope we'd be thinking carefully enough about it to consider switching the loop type :-)

@njx
Copy link
Contributor

njx commented Jun 21, 2012

Yup. In any case, the original bug still stands :)

@pthiess
Copy link
Contributor

pthiess commented Jun 22, 2012

Reviewed - @RaymondLim - Ray please work with Peter on this bug.

@RaymondLim
Copy link
Contributor

Move to sprint 12 as we need some more cleanup pointed out by @peterflynn in #1287.

@peterflynn
Copy link
Member

Moving to Sprint 13 -- Raymond won't have time to finish this, and it's just a non-critical code cleanup

@RaymondLim
Copy link
Contributor

This requires changes to many files. So moving to the next sprint again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants