0x00 方法
Math.ceil(x/y);
Math.floor(x/y);
Math.round(x/y);
parseInt(x/y);
0x01 性能
function fn1(x, y){let t = Math.ceil(x/y)}
function fn2(x, y){let t = Math.floor(x/y)}
function fn3(x, y){let t = Math.round(x/y)}
function fn4(x, y){let t = parseInt(x/y)}
function test(fn, n){
let start = new Date().getTime();
for(let i=0; i<n; i++){fn(i, 137437)}
let end = new Date().getTime();
console.log(n, end-start)
}
test(fn1, 10000000);
test(fn2, 10000000);
test(fn3, 10000000);
test(fn4, 10000000);
0x02 附
- 个人是个菜鸟,测试准确性不敢保证;
- 测试使用的浏览器内核:Chromium 71.0.3578.98 (64位)
- 测试时间:Sun Mar 10 2019 10:16:41 GMT+0800 (中国标准时间)
- 因准确性不敢保证,因此转载请注明出处以便溯源