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 (中國標準時間)
- 因準確性不敢保證,因此轉載請註明出處以便溯源