The primary reason Math.floor is slower (where it actually is--in some tests I've done it's faster) is that it involves a function call. Older JavaScript implementations couldn't inline function calls. Newer engines can inline the call, or at least make the property lookup faster, but they still need a guard condition in case you (or some other script) overwrote the Math.floor function. The overhead is minimal though, so there's not much difference in speed.

More importantly though, as was mentioned in several comments, the other methods are not equivalent. They all work by doing bitwise operations. The bitwise operators automatically convert their operands to 32-bit integers by truncating the number. That's fine if the number fits in 32 bits, but JavaScript numbers are 64-bit floats, which could be much larger than 2147483647.

They also give a different result for negative numbers, since converting to integers truncates and Math.floor always rounds down. For example, Math.floor(-2.1) === -3, but (-2.1) | (-2.1) === -2.

If you know you are only dealing with positive numbers less than 2147483648, and you need to squeeze every bit of performance out of your code in older browsers (Make sure it's actually the bottleneck first. It probably isn't.), I would use an even simpler method: x|0. It doesn't evaluate the variable twice, and it works even if x is an expression (just be sure to put it in parentheses so you don't run into precedence issues).

Answer from Matthew Crumley on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
JavaScript Math floor() Method
The Math.floor() method rounds a number DOWN to the nearest integer. The Math.abs() Method The Math.ceil() Method The Math.floor() Method The Math.round() Method The Math.fround() Method The Math.f16round() Method The Math.trunc() Method ...
Discussions

optimization - Why is Javascript's Math.floor the slowest way to calculate floor in Javascript? - Stack Overflow
This seems pretty shocking as I ... Javascript in today's modern browsers would be some pretty smart people. Does floor do something important that the other methods fail to do? Is there any reason to use it? ... ~~n, n|n and n&n does not produce the same result as Math.fl... More on stackoverflow.com
🌐 stackoverflow.com
Why not always use Math.round instead of Math.floor?

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

More on reddit.com
🌐 r/javascript
13
1
January 8, 2016
javascript - What exactly does Math.floor do? - Stack Overflow
Math.floor always rounds to the nearest whole number that is smaller than your input. You might confuse it with Math.round, that rounds to nearest whole number. This is why this code always outputs 1 or 0, since input never gets to 2 or bigger: ... There's actually three different rounding functions in JavaScript... More on stackoverflow.com
🌐 stackoverflow.com
Math.floor vs Math.round vs parseInt vs Bitwise jsPerf
What I find interesting is how fast the bitwise stuff is--Crockford said something about not using them in The Good Parts™ because they are so slow. More on reddit.com
🌐 r/javascript
31
21
July 18, 2012
People also ask

What is the purpose of JavaScript's Math.floor() function?
It rounds down floating-point numbers to the nearest integer that is less than or equal to the original value.
🌐
scaler.com
scaler.com › home › topics › javascript math.floor() function
JavaScript math.floor() Function with Examples - Scaler Topics
How can I convert decimals to integers using Math.floor()?
Simply provide your decimal value as an argument to `Math.floor()` and it will return its truncated integer equivalent value.
🌐
scaler.com
scaler.com › home › topics › javascript math.floor() function
JavaScript math.floor() Function with Examples - Scaler Topics
Are there any comparable functions in JavaScript to round numbers?
Yes, JavaScript provides two similar rounding functions - `Math.ceil()` for rounding to the nearest integer greater than or equal to the given number, and `Math.round()` with standard rounding rules.
🌐
scaler.com
scaler.com › home › topics › javascript math.floor() function
JavaScript math.floor() Function with Examples - Scaler Topics
🌐
TutorialsPoint
tutorialspoint.com › javascript › math_floor.htm
JavaScript Math.floor() Method
The Math.floor() method in JavaScript accepts a numeric value as an argument, rounds down and returns the largest integer less than or equal to the provided number. For instance, if we provide "5.6" as an argument to this method, it returns "5" as a
Top answer
1 of 2
37

The primary reason Math.floor is slower (where it actually is--in some tests I've done it's faster) is that it involves a function call. Older JavaScript implementations couldn't inline function calls. Newer engines can inline the call, or at least make the property lookup faster, but they still need a guard condition in case you (or some other script) overwrote the Math.floor function. The overhead is minimal though, so there's not much difference in speed.

More importantly though, as was mentioned in several comments, the other methods are not equivalent. They all work by doing bitwise operations. The bitwise operators automatically convert their operands to 32-bit integers by truncating the number. That's fine if the number fits in 32 bits, but JavaScript numbers are 64-bit floats, which could be much larger than 2147483647.

They also give a different result for negative numbers, since converting to integers truncates and Math.floor always rounds down. For example, Math.floor(-2.1) === -3, but (-2.1) | (-2.1) === -2.

If you know you are only dealing with positive numbers less than 2147483648, and you need to squeeze every bit of performance out of your code in older browsers (Make sure it's actually the bottleneck first. It probably isn't.), I would use an even simpler method: x|0. It doesn't evaluate the variable twice, and it works even if x is an expression (just be sure to put it in parentheses so you don't run into precedence issues).

2 of 2
27

It has nothing to do with modern browsers. It has to do with implementing the ECMA standard. You can't just change how a certain function performs even if there is a faster way. It could break existing code.

The Math.Floor has to account for a lot of different scenarios of handling different types. Could they have made different scenarios faster by taking short cuts as you described? Maybe they could, but that might have broken other scenarios. Just because something on the surface looks small, doesn't mean that there isn't an iceberg underneath.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-floor-method
JavaScript Math floor() Method - GeeksforGeeks
November 7, 2024 - The Math.floor() method in JavaScript is used to round off a number down to the nearest integer, moving towards the lower value.
Find elsewhere
🌐
Reddit
reddit.com › r/javascript › why not always use math.round instead of math.floor?
r/javascript on Reddit: Why not always use Math.round instead of Math.floor?
January 8, 2016 -

When I read through the code of colleagues and public repos, I see Math.floor used like 20x more often than Math.round.

But why? Isn't Math.round more accurate than Math.floor? Shouldn't it be the other way around (using Math.round more often than Math.floor)?

Is Math.floor so much faster than Math.round or am I missing something?

Edit

I am aware that those two do different things. My point is that in my experience, Math.floor is much too often used, when Math.round would simply be more accurate.

Top answer
1 of 6
9

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

2 of 6
3

Math.floor - You have a rating system of stars, and you aren't breaking them up into half stars. You do a query to get all the votes and the math comes back to 4.7 stars. You would use Math.floor here so that you display 4 stars.

 

Math.ceil - You have a slider module that displays 3 slides at a time. This module contains 19 slides. 19/3 = 6.33. If you were to floor or round here you would end up with 6. But to make sure that all 19 slides are shown, you need 7 containers, so you use Math.ceil.

 

Math.round - Anytime you need the closest number without worrying about anything like the above scenarios.

🌐
Scaler
scaler.com › home › topics › javascript math.floor() function
JavaScript math.floor() Function with Examples - Scaler Topics
January 2, 2024 - The function will take this input and return the largest integer that is less than or equal to "x." JavaScript's Math.floor() function returns the largest whole number that is less than or equal to the input number; specifically, it truncates ...
🌐
SheCodes
shecodes.io › athena › 57992-what-does-math-floor-do-in-javascript
[JavaScript] - What does Math.floor() do in JavaScript? - | SheCodes
Learn about the Math.floor() method in JavaScript and how it rounds down a given number to its nearest integer.
🌐
Math.js
mathjs.org › docs › reference › functions › floor.html
math.js | an extensive math library for JavaScript and Node.js
math.floor(c, 1) // returns Complex 3.2 -2.8i const unit = math.unit('3.241 cm') const cm = math.unit('cm') const mm = math.unit('mm') math.floor(unit, 1, cm) // returns Unit 3.2 cm ...
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_math_floor_r.html
floor (JavaScript)
Rounds a number to an integer by truncating the fraction. Math (JavaScript) floor(value:double) : double · Special cases are as follows: Rounding NaN results in NaN. Rounding +Infinity results in positive infinity. Rounding -Infinity results in negative infinity.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › floor
JavaScript Math floor() - Round Down Value | Vultr Docs
November 29, 2024 - The Math.floor() function in JavaScript is a crucial tool for rounding down numerical values to the nearest whole number.
🌐
Codecademy
codecademy.com › forum_questions › 50c386a4a122749bc1006ca6
Math.random and Math.floor explained | Codecademy
Math.random generates a number ... To get it to be a whole number, i.e. an integer, apply Math.floor, which rounds down to the nearest whole number: Math.floor(Math.random() * 10) To get a whole number between 1 and 10, add ...
🌐
Educative
educative.io › answers › mathceil-mathfloor-and-mathround-in-javascript
Math.ceil, Math.floor, and Math.round in JavaScript
So, if we have a value of 1.4, ... greater than or equal to the value we pass, Math.floor returns the largest or equal integer that is less than the given value....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
function getRandomInt(max) { return ... 0 console.log(Math.random()); // Expected output: a number from 0 to <1 ... A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive)....
🌐
W3Schools
w3schools.com › js › js_math.asp
JavaScript Math Object
There are 4 common methods to round a number to an integer: ... Math.floor(4.9); Math.floor(4.7); Math.floor(4.4); Math.floor(4.2); Math.floor(-4.2); Try it Yourself » ... Math.trunc(4.9); Math.trunc(4.7); Math.trunc(4.4); Math.trunc(4.2); Math.trunc(-4.2); Try it Yourself » · Math.sign(x) returns if x is negative, null or positive. ... Math.trunc() and Math.sign() were added to JavaScript 2015 - ES6.
🌐
Flexiple
flexiple.com › javascript › javascript-math-floor
Javascript Math.floor() - uses and limitations - Flexiple
March 14, 2022 - Learn how to use Javascript's Math.floor() function and understand its limitations. Get insights on rounding numbers down efficiently and accurately.
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
Math.floor JavaScript: Avoid Rounding Mistakes | Code Highlights
December 20, 2023 - Have you ever faced a situation in your coding journey where precise number rounding was crucial? Maybe you're calculating currency or setting up a game score system, and you need to round down to the nearest whole number. That's where Math.floor in JavaScript comes into play, ensuring your numbers aren't just rounded—they're rounded correctly.
🌐
JavaScript in Plain English
javascript.plainenglish.io › math-ceil-math-round-and-math-floor-in-your-javascript-7142fde2c56d
Math.ceil, Math.round and Math.floor in Your JavaScript | by Piero Borrelli | JavaScript in Plain English
September 9, 2020 - Luckily for us, a language like JavaScript gives us tools like the Math object to deal with numbers and their operations. When it comes to rounding numbers, Math helps you with three main functions: Math.ceil, Math.floorand Math.round. This method rounds up the only passed in value to the nearest greater integer.
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596101996 › re110.html
Math.floor( ): round a number down — ECMAScript v1 - JavaScript: The Definitive Guide, 5th Edition [Book]
August 17, 2006 - JavaScript from Flash23.4.5. Example: Flash to JavaScript, and Back to Flash23.5. Scripting Flash 8 ... Any numeric value or expression. The closest integer less than or equal to x. Math.floor( ) computes the floor function; in other words, ...
Author   David Flanagan
Published   2006
Pages   1018