哪个更高效array.forEach或angular.forEach?

时间:2020-12-11 02:32:33

array.prototype.forEach

forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays).

forEach()按升序为数组中的每个元素执行一次提供的回调。对于已删除或未初始化的索引属性(即在稀疏数组上),不会调用它。

Source: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

资料来源:https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

angular.forEach

Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key, obj), where value is the value of an object property or an array element, key is the object property key or array element index and obj is the obj itself. Specifying a context for the function is optional.

为obj集合中的每个项目调用迭代器函数一次,可以是对象或数组。使用iterator(value,key,obj)调用迭代器函数,其中value是对象属性或数组元素的值,key是对象属性键或数组元素索引,obj是obj本身。指定函数的上下文是可选的。

Source: https://docs.angularjs.org/api/ng/function/angular.forEach

资料来源:https://docs.angularjs.org/api/ng/function/angular.forEach

But I want to know which one is more efficient and the performance.

但我想知道哪一个更有效率和性能。

1 个解决方案

#1


9  

AngularJS forEach used to implement ES5 forEach if available, which was not the fastest, but since this commit it is using the fastest for loop.

AngularJS forEach用于实现ES5 forEach(如果可用),这不是最快的,但是自提交以来它使用最快的for循环。

https://angularjs.de/buecher/angularjs-cookbook/es5-array-functions

https://angularjs.de/buecher/angularjs-cookbook/es5-array-functions

If you look at this comparison, you see, that the ES5 forEach implementation is not the fastest. The AngularJS version in this comparison uses ES5 forEach, if it’s available. This is changed by this commit. Now it’s always using the fastest for loop.

如果你看一下这个比较,你会发现,ES5 forEach实现并不是最快的。此比较中的AngularJS版本使用ES5 forEach(如果可用)。这个提交改变了这一点。现在它始终使用最快的for循环。

#1


9  

AngularJS forEach used to implement ES5 forEach if available, which was not the fastest, but since this commit it is using the fastest for loop.

AngularJS forEach用于实现ES5 forEach(如果可用),这不是最快的,但是自提交以来它使用最快的for循环。

https://angularjs.de/buecher/angularjs-cookbook/es5-array-functions

https://angularjs.de/buecher/angularjs-cookbook/es5-array-functions

If you look at this comparison, you see, that the ES5 forEach implementation is not the fastest. The AngularJS version in this comparison uses ES5 forEach, if it’s available. This is changed by this commit. Now it’s always using the fastest for loop.

如果你看一下这个比较,你会发现,ES5 forEach实现并不是最快的。此比较中的AngularJS版本使用ES5 forEach(如果可用)。这个提交改变了这一点。现在它始终使用最快的for循环。