console
let createArray = (min, max) => {
let r = []
let i = min
while (i <= max) {
r.push(i)
i++
}
return r
}
function start1() {
console.time('第一种用时')
let a = createArray(1, 50000)
let b = createArray(25000, 100000)
let common = []
let aPoint = 0
let bPoint = 0
while (aPoint < a.length) {
if (a[aPoint] === b[bPoint]) {
common.push(a[aPoint])
aPoint++
bPoint++
} else if (a[aPoint] < b[bPoint]) {
aPoint++
} else if (a[aPoint] > b[bPoint]) {
bPoint++
}
}
console.timeEnd('第一种用时')
}
function start2() {
console.time('第二种用时')
let c = createArray(1, 50000)
let d = createArray(25000, 100000)
let common2 = []
c.forEach(item => {
d.forEach(_item => {
if (item === _item) {
common2.push(item)
}
})
})
console.timeEnd('第二种用时')
}
let timer
let num = 0
function clock() {
clearTimeout(timer)
timer = setTimeout(() => {
num++
document.body.append(num)
clock()
}, 1000)
}
clock()
<button onclick="start1()">算法1</button>
<button onclick="start2()">算法2</button>