Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between javascript foreach loop, for..in and angular forEach loop.?

Tags:

Iam new to angular framework. when i want to iterate json object in angular, i used javascript foreach and for..in loops.

Later i came to know that angular itself has a angular.forEach loop to iterate objects.

How can i compare performance of angular.forEach with javascript for..in and foreach loops??

Why we should use angular.forEach instead of javascript foreach and for..in??

Please give me some examples and reasons to use it, which shows the performance.

Thanks :)

like image 779
Ravi Teja Kumar Isetty Avatar asked Aug 29 '16 18:08

Ravi Teja Kumar Isetty


People also ask

Which is better for loop or forEach in JavaScript?

forEach is almost the same as for or for..of , only slower. There's not much performance difference between the two loops, and you can use whatever better fit's the algorithm. Unlike in AssemblyScript, micro-optimizations of the for loop don't make sense for arrays in JavaScript.

Can you describe the main difference between a .forEach loop and a map () loop and why you would pick one versus the other?

Differences between forEach() and map() methods:The forEach() method does not create a new array based on the given array. The map() method creates an entirely new array. The forEach() method returns “undefined“. The map() method returns the newly created array according to the provided callback function.

What is the difference between forEach and each?

each differs for the arguments passed to the callback. If you use _. forEach , the first argument passed to the callback is the value, not the key. So if you don't bother at all about the key you should use _.


2 Answers

Angular forEach - Invokes the iterator function once for each item in obj collection, which can be either an object or an array.

var values = {name: 'misko', gender: 'male'}; angular.forEach(values, function(value, key) {   console.log(key + ': ' + value); });  // Output: // "name: misko" // "gender: male" 

for..in - iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

var obj = {a:1, b:2, c:3};  for (var prop in obj) {   console.log("obj." + prop + " = " + obj[prop]); }  // Output: // "obj.a = 1" // "obj.b = 2" // "obj.c = 3" 

forEach - method executes a provided function once per array element.

// Notice that index 2 is skipped since there is no item at // that position in the array. [2, 5, , 9].forEach(function (element, index, array) {   console.log('a[' + index + '] = ' + element); }); // logs: // a[0] = 2 // a[1] = 5 // a[3] = 9 

In terms of performance it depends on the data structure you're working with, if it's an Array I will suggest using Angular.forEach or native forEach, if it's an Object for..in will be the best, however it seems Angular.forEach handles object pretty well too. Depending on the amount of data you working with. If it's enormous I will suggest you use libraries like Lodash or Underscore, they handle data manipulation well.

like image 86
Akinjide Avatar answered Oct 01 '22 16:10

Akinjide


angular.forEach is basically a polyfill.

Therefore, if you are using angular, it doesn't matter if the browser is older because angular will provide a replacement if it's required.

In code it would look like this:

if (!Array.prototype.forEach) {     Array.prototype.forEach = function(...) {         // angulars own implementation     } } 

There are some other differences, e.g.

  • angular.forEach supports iteration over objects;
  • angular.forEach takes an object/array as it's first argument.
like image 42
chriskelly Avatar answered Oct 01 '22 16:10

chriskelly