是否有一种简单的方法可以将2中的商2从10%中减去?我是否必须手动重新计算
21-21%10 答案 0 :(得分:0) 使用 如果代码速度真的是一个问题(几乎总是没有),尤其是对于以10为底的数字,除以然后再使用 1 个答案:
/
代替%
,然后向下舍入:console.log(
Math.floor(21 / 10)
);
Math.floor
看起来比将数字转化为整数要快得多。一个字符串,并获取除前一位以外的所有数字:const length = 10_000_000;
const bigArr = Array.from(
{ length },
() => Math.ceil(Math.random() * 100)
);
const smallArr = Array.from(
{ length },
() => 10
);
setTimeout(() => {
const t0 = performance.now();
for (let i = 0; i < length; i++) {
const quotient = Math.floor(bigArr[i] / smallArr[i]);
}
const t1 = performance.now();
console.log(t1 - t0);
}, 1000);
const length = 10_000_000;
const bigArr = Array.from(
{ length },
() => Math.ceil(Math.random() * 100)
);
const smallArr = Array.from(
{ length },
() => 10
);
setTimeout(() => {
const t0 = performance.now();
for (let i = 0; i < length; i++) {
const str = String(bigArr[i])
const quotient = str.slice(0, str.length - 1);
}
const t1 = performance.now();
console.log(t1 - t0);
}, 1000);