美文网首页
for of, for in, forEach... test

for of, for in, forEach... test

作者: 空天啸鹤 | 来源:发表于2020-04-28 23:19 被阅读0次
let time = Date.now();
//        -- new line --           
time = Date.now();
let total = 0;
for (let i = 0; i < 100000; i += 1) {
    total += i;
}
console.log('for total:', Date.now() - time)
VM480:6 for total: 3
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM526:6 for total: 499999500000 15
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM529:6 for total: 499999500000 18
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM532:6 for total: 499999500000 14
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM535:6 for total: 499999500000 15
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM538:6 for total: 499999500000 16
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM541:6 for total: 499999500000 15
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM544:6 for total: 499999500000 15
//        -- new line --           
time = Date.now();
total = 0;
for (let i = 0; i < 1000000; i += 1) {
    total += i;
}
console.log('for total:', total, Date.now() - time)
VM547:6 for total: 499999500000 16
//        -- new line --           
time = Date.now();
total = 0;
let i = 0;
while( i < 1000000){
    total += i;
    i += 1;
}
console.log('for total:', total, Date.now() - time)
VM623:8 for total: 499999500000 15
//        -- new line --           
time = Date.now();
total = 0;
let i = 0;
while( i < 1000000){
    total += i;
    i += 1;
}
console.log('for total:', total, Date.now() - time)
VM628:1 Uncaught SyntaxError: Identifier 'i' has already been declared
    at <anonymous>:1:1
(anonymous) @ VM628:1
time = Date.now();
total = 0;
i = 0;
while( i < 1000000){
    total += i;
    i += 1;
}
console.log('while total:', total, Date.now() - time)
VM641:8 while total: 499999500000 16
//        -- new line --           
time = Date.now();
total = 0;
i = 0;
while( i < 1000000){
    total += i;
    i += 1;
}
console.log('while total:', total, Date.now() - time)
VM644:8 while total: 499999500000 17
//        -- new line --           
let arr = new Array(10)
//        -- new line --           
arr.length
10
arr = new Array(100000)
(100000) [empty × 100000]
time = Date.now();
total = 0;
for (let item in arr) {
    if (item) {
        total += 1;
    }
}
console.log('for in total:', total, Date.now() - time)
VM952:8 for in total: 0 0
//        -- new line --           
time = Date.now();
total = 0;
arr.forEach(item => {
    if (item) {
        total += 1;
    }
})
console.log('for in total:', total, Date.now() - time)
VM1005:8 for in total: 0 1
//        -- new line --           
time = Date.now();
total = 0;
for(let item of arr) {
    if (item) {
        total += 1;
    }
})
console.log('for of total:', total, Date.now() - time)
VM1045:7 Uncaught SyntaxError: Unexpected token ')'
time = Date.now();
total = 0;
for(let item of arr) {
    if (item) {
        total += 1;
    }
}
console.log('for of total:', total, Date.now() - time)
VM1051:8 for of total: 0 5
//        -- new line --           
time = Date.now();
total = 0;
for(let i = 0; i < arr.length; i += 1) {
    if (arr[i]) {
        total += 1;
    }
}
console.log('for let total:', total, Date.now() - time)
VM1110:8 for let total: 0 3
//        -- new line --           
arr = new Array(10000000)
(10000000) [empty × 10000000]
time = Date.now();
total = 0;
for(let i = 0; i < arr.length; i += 1) {
    if (arr[i]) {
        total += 1;
    }
}
console.log('for let total:', total, Date.now() - time)
VM1222:8 for let total: 0 143
//        -- new line --           
time = Date.now();
total = 0;
for(let item of arr) {
    if (item) {
        total += 1;
    }
}
console.log('for of total:', total, Date.now() - time)
VM1229:8 for of total: 0 159
//        -- new line --           
time = Date.now();
total = 0;
arr.forEach(item => {
    if (item) {
        total += 1;
    }
})
console.log('for in total:', total, Date.now() - time)
VM1239:8 for in total: 0 27
//        -- new line --           
time = Date.now();
total = 0;
for (let item in arr) {
    if (item) {
        total += 1;
    }
}
console.log('for in total:', total, Date.now() - time)
VM1264:8 for in total: 0 9
//        -- new line --           
time = Date.now();
total = 0;
let run = 0;
for (let item in arr) {
run += 1;
    if (item) {
        total += 1;
    }
}
console.log('for in total:', total, run, Date.now() - time)
VM1310:10 for in total: 0 0 6
//        -- new line --           
time = Date.now();
total = 0; 
run = 0;
arr.forEach(item => {
    run += 1;
    if (item) {
        total += 1;
    }
})
console.log('for in total:', total, run, Date.now() - time)
VM1373:10 for in total: 0 0 27
//        -- new line --           
time = Date.now();
total = 0; run = 0;
for(let item of arr) {
run += 1;
    if (item) {
        total += 1;
    }
}
console.log('for of total:', run, total, Date.now() - time)
VM1424:9 for of total: 10000000 0 163
//        -- new line --           
time = Date.now();
total = 0; run = 0;
arr.map( item => {
run += 1;
    if (item) {
        total += 1;
    }
})
console.log('for of total:', run, total, Date.now() - time)
VM1579:9 for of total: 0 0 73
//        -- new line --           
time = Date.now();
total = 0; run = 0;
arr.filter( item => {
run += 1;
    if (item) {
        total += 1;
    }
})
console.log('for f total:', run, total, Date.now() - time)
VM1594:9 for f total: 0 0 31

相关文章

网友评论

      本文标题:for of, for in, forEach... test

      本文链接:https://www.haomeiwen.com/subject/jfaiwhtx.html