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
๐ŸŒ
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
February 22, 2017
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 google.com
๐ŸŒ google.com
13
1
February 22, 2017
You can do integer division in JavaScript
It took me longer than I'd like to understand. heavy sigh More on reddit.com
๐ŸŒ r/shittyprogramming
69
295
March 15, 2015
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?
February 22, 2017 -

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.
๐ŸŒ
Google
google.com โ€บ goto
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
December 23, 2012 - Math.random generates a number ... 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 1 to the answer: Math.floor(Math.random() ...
๐ŸŒ
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.