最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can Array.indexOf be more efficient than Array.some - Stack Overflow

programmeradmin3浏览0评论

This question is inspired from peting answers on this question: indexOf with multiple arguments

The user wanted to know an efficient way to test an array for the existence of one or more integers given in an array. Specifically, given an array and the numbers 123, 124, and 125, how can you tell if one or more of these integers exists in the given array. Two solutions were suggested:

Using indexOf():

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var exists = array.indexOf(123) !== -1 || array.indexOf(124) !== -1 || array.indexOf(125) !== -1;

Or, using some():

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var exists = array.some(function(item) {
  return item === 123 || item === 124 || item === 125;
});

Both the ECMA-262 algorithms for indexOf() and some() short-circuit when finding a successful match, but I would have thought that the some() implementation would be faster when there are no matches. However, another user pointed out that the indexOf() solution is faster.

How is the indexOf() code more efficient even though it must iterate over the array more times?

This question is inspired from peting answers on this question: indexOf with multiple arguments

The user wanted to know an efficient way to test an array for the existence of one or more integers given in an array. Specifically, given an array and the numbers 123, 124, and 125, how can you tell if one or more of these integers exists in the given array. Two solutions were suggested:

Using indexOf():

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var exists = array.indexOf(123) !== -1 || array.indexOf(124) !== -1 || array.indexOf(125) !== -1;

Or, using some():

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var exists = array.some(function(item) {
  return item === 123 || item === 124 || item === 125;
});

Both the ECMA-262 algorithms for indexOf() and some() short-circuit when finding a successful match, but I would have thought that the some() implementation would be faster when there are no matches. However, another user pointed out that the indexOf() solution is faster.

How is the indexOf() code more efficient even though it must iterate over the array more times?

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Aug 17, 2016 at 21:53 DaveDave 10.9k3 gold badges44 silver badges54 bronze badges 2
  • 1 I would imagine because .indexOf lacks the overhead of calling a function. some is probably more efficient for larger arrays. – Blender Commented Aug 17, 2016 at 21:56
  • 1 Using different browsers, and running tests several times, I got different results. On Chrome 52, test2 is faster. On Firefox 50, test1 is faster. Not so sure then that one is faster than the other. – nioKi Commented Aug 17, 2016 at 22:03
Add a ment  | 

1 Answer 1

Reset to default 7

The thing is - your question is highly relies not on the standard itself, but on how the standard is implemented.

Said that, the results may be not consistent between different engines.

The very obvious assumption that "there is an overhead to call a function" some time in some engine might be mitigated by inlining a function call and some other tricky optimisations.

To summarise: there is no single right answer to that, and any answer that would not use any references to the ES implementations + runtime details (cpu instructions/opcodes, optimisations used) - is simply a speculation.

发布评论

评论列表(0)

  1. 暂无评论