let time = Date.now();
let total = 0;
for (let i = 0; i < 100000; i += 1) {
total += i;
}
console.log('for 100000-0:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let i = 0; i < 100000; i += 1) {
total += i;
}
console.log('for 100000-1:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let i = 0; i < 1000000; i += 1) {
total += i;
}
console.log('for 1000000-0:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let i = 0; i < 1000000; i += 1) {
total += i;
}
console.log('for 1000000-1:', Date.now() - time, ' calcResult:', total)
let i = 0;
total = 0;
time = Date.now();
while( i < 1000000){
total += i;
i += 1;
}
console.log('while 1000000-0:', Date.now() - time, ' calcResult:', total)
i = 0
total = 0;
time = Date.now();
while( i < 1000000){
total += i;
i += 1;
}
console.log('while 1000000-1:', Date.now() - time, ' calcResult:', total)
arr = new Array(1000000)
total = 0;
time = Date.now();
for (let item in arr) {
total += 1;
}
console.log('for in 1000000-0:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let item in arr) {
total += 1; // 请注意这里,空元素数组,不会进入其中;
}
console.log('for in 1000000-1:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let item in arr) {
total += 1; // 请注意这里,空元素数组,不会进入其中;
}
console.log('for in 1000000-1:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
for(let item of arr) {
total += 1; // 请注意这里,空元素数组,会进入其中
}
console.log('for of 1000000-1:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.forEach(item => {
total += 1;
})
console.log('forEach 1000000-0:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.map(item => {
total += 1;
})
console.log('map 1000000-0:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.filter(item => {
total += 1;
})
console.log('filter 1000000-0:', Date.now() - time, ' calcResult:', total)
结果:
VM532:10 for 100000-0: 3 calcResult: 4999950000
VM532:13 for 100000-1: 1 calcResult: 4999950000
VM532:20 for 1000000-0: 14 calcResult: 499999500000
VM532:27 for 1000000-1: 11 calcResult: 499999500000
VM532:36 while 1000000-0: 15 calcResult: 499999500000
VM532:45 while 1000000-1: 15 calcResult: 499999500000
VM532:53 for in 1000000-0: 1 calcResult: 0
VM532:60 for in 1000000-1: 1 calcResult: 0
VM532:67 for in 1000000-1: 1 calcResult: 0
VM532:74 for of 1000000-1: 29 calcResult: 1000000
VM532:81 forEach 1000000-0: 2 calcResult: 0
VM532:88 map 1000000-0: 3 calcResult: 0
VM532:95 filter 1000000-0: 1 calcResult: 0
测试arr有值时结果:
// ------ 2 ------
let arr = new Array(1000000)
let i = 0
let time = Date.now();
while( i < arr.length){
arr[i] = i
i += 1;
}
console.log('set arr 1000000-0:', Date.now() - time )
let total = 0
time = Date.now();
for (let i = 0; i < arr.length; i += 1) {
total += arr[i];
}
console.log('for 100000-0:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let item in arr) {
total += arr[item]; // 请注意这里,空元素数组,不会进入其中;
}
console.log('for in 1000000-0:', Date.now() - time, ' calcResult:', total)
total = 0;
time = Date.now();
for (let item in arr) {
total += arr[item]; // 请注意这里,空元素数组,不会进入其中;
}
console.log('for in 1000000-1:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
for(let item of arr) {
total += item; // 请注意这里,空元素数组,会进入其中
}
console.log('for of 1000000-1:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.forEach(item => {
total += item;
})
console.log('forEach 1000000-0:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.map(item => {
total += item;
})
console.log('map 1000000-0:', Date.now() - time, ' calcResult:', total)
time = Date.now();
total = 0;
arr.filter(item => {
total += item;
})
console.log('filter 1000000-0:', Date.now() - time, ' calcResult:', total)
结果:
VM755:10 set arr 1000000-0: 6
VM755:17 for 100000-0: 15 calcResult: 499999500000
VM755:24 for in 1000000-0: 270 calcResult: 499999500000
VM755:31 for in 1000000-1: 226 calcResult: 499999500000
VM755:38 for of 1000000-1: 21 calcResult: 499999500000
VM755:45 forEach 1000000-0: 16 calcResult: 499999500000
VM755:52 map 1000000-0: 20 calcResult: 499999500000
VM755:59 filter 1000000-0: 16 calcResult: 499999500000