1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| const foo = { count: 1, } function func(name, a, b, c, d) { return name + this.count }
function call(args) { const t = Date.now() for (let i = 0; i < 1000000; i++) { switch (args.length) { case 0: func.call(foo) break case 1: func.call(foo, args[0]) break case 2: func.call(foo, args[0], args[1]) break case 3: func.call(foo, args[0], args[1], args[2]) break
case 4: func.call(foo, args[0], args[1], args[2], args[3]) break case 5: func.call(foo, args[0], args[1], args[2], args[3], args[4]) break } } const r = Date.now() - t return r } function apply(args) { const t = Date.now() for (let i = 0; i < 1000000; i++) { switch (args.length) { case 0: func.apply(foo) break case 1: func.apply(foo, args) break case 2: func.apply(foo, args) break case 3: func.apply(foo, args) break case 4: func.apply(foo, args) break case 5: func.apply(foo, args) break } } const r = Date.now() - t return r } const arr1 = [0, 0, 0, 0, 0, 0] const arr2 = [0, 0, 0, 0, 0, 0] for (let i = 0; i < 100; i++) { arr1[0] += call([1]) arr1[1] += call([1, 2]) arr1[2] += call([1, 2, 3]) arr1[3] += call([1, 2, 3, 4]) arr1[4] += call([1, 2, 3, 4, 5])
arr2[0] += apply([1]) arr2[1] += apply([1, 2]) arr2[2] += apply([1, 2, 3]) arr2[3] += apply([1, 2, 3, 4]) arr2[4] += apply([1, 2, 3, 4, 5]) } arr1[0] /= 100 arr1[1] /= 100 arr1[2] /= 100 arr1[3] /= 100 arr1[4] /= 100
arr2[0] /= 100 arr2[1] /= 100 arr2[2] /= 100 arr2[3] /= 100 arr2[4] /= 100
for (let i = 0; i < 5; i++) { console.log(`${i + 1}个参数调用call 循环一百万 * 100 次平均消耗${arr1[i]}ms`) console.log(`${i + 1}个参数调用apply 循环一百万 * 100 次平均消耗${arr2[i]}ms`) }
|