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

javascript - Faster loop: foreach vs some (performance of jsperf is different than node or chrome) - Stack Overflow

programmeradmin3浏览0评论

Which is the best way to resume the value of an array into simple true or false values.

I am quite confused as jsperf is giving me VERY different results than what google chrome console, nodejs, or any other JS engine gives me. (jsperf snippet here)

This is the code snippet, and you can see (you can run it here) that some is like 100 times faster than using a foreach loop

var array = [];
var i = 0;
var flag = false;
while (i< 100000) {
    array.push(Math.random()*10000);
    i++;
}

console.time('forEach');
array.forEach((item) => {

    if (!flag && item > 10000/2) {
        flag = true;
        return;
    }
    return false
});
console.timeEnd('forEach');
console.log(flag);

flag = false;
console.time('some');
flag = array.some((item) => {

    if (item > 10000/2) {
        return true;
    }
    return false
});
console.timeEnd('some');
console.log(flag);

Which is the best way to resume the value of an array into simple true or false values.

I am quite confused as jsperf is giving me VERY different results than what google chrome console, nodejs, or any other JS engine gives me. (jsperf snippet here)

This is the code snippet, and you can see (you can run it here) that some is like 100 times faster than using a foreach loop

var array = [];
var i = 0;
var flag = false;
while (i< 100000) {
    array.push(Math.random()*10000);
    i++;
}

console.time('forEach');
array.forEach((item) => {

    if (!flag && item > 10000/2) {
        flag = true;
        return;
    }
    return false
});
console.timeEnd('forEach');
console.log(flag);

flag = false;
console.time('some');
flag = array.some((item) => {

    if (item > 10000/2) {
        return true;
    }
    return false
});
console.timeEnd('some');
console.log(flag);

The question is, Why is JSPERF giving different results than chrome's console, nodejs, or any other JS engine?

EDIT: As stated by my answer to the question underneath, the behaviour was buggy, because I had the chrome dev tools open when using JSPERF, and all of the messages were being logged to the console, which means that actually the results changed. Keep in mind for the future, that JSPERF might not behave properly when leaving the console open on execution.

Share Improve this question edited Mar 9, 2019 at 11:15 Alejandro Vales asked Jun 20, 2018 at 15:05 Alejandro ValesAlejandro Vales 2,9371 gold badge24 silver badges43 bronze badges 1
  • 1 You can measure the execution time of your above code and see for yourself :) – Yotam Omer Commented Jun 20, 2018 at 15:08
Add a comment  | 

2 Answers 2

Reset to default 16

from MDN

There is no way to stop or break a forEach() loop other than by throwing an exception.

using a foreach, the loop will be executed exactly 100000 times. using some, the loop stops as soon your predicate returns true.

as long as there is a chance your predicate is true, some will be more efficient

After researching a bitI understood what did I do to make jsperf behave in a strange manner. I had the chrome console open when running the jsperf tests

I have seen that when you open the chrome console jsperf still logs console.log and similar messages while the scripts are executing and that is what was causing the misleading result of the tests.

Here you can see the tets after CLOSING the console window...

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论