DEV Community

Cover image for Stop Using parseInt - Here's Why It's Slowing You Down
Werliton Silva
Werliton Silva

Posted on

Stop Using parseInt - Here's Why It's Slowing You Down

Have you ever used parseInt to truncate a number in JavaScript? I used to do it too. But today, I want to show you why that habit might be hurting your code - and how to do it better.

๐Ÿง  The Problem

parseInt was designed to convert strings to integers, not to truncate numbers. When you write:

parseInt(4.9); // 4 
Enter fullscreen mode Exit fullscreen mode

Does it work? Yes.
Is it clear? Not really.
Is it fast? Definitely not.


โšก๏ธ The Better Way

If you're working with a number and want to remove the decimal part, use:

Math.trunc(4.9); // 4 
Enter fullscreen mode Exit fullscreen mode

It's faster, clearer, and semantically correct. It also works with negative numbers:

Math.trunc(-4.9); // -4 
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Performance Matters

In simple benchmarks, Math.trunc can be up to 5x faster than parseInt. That matters - especially in loops, animations, or real-time calculations.


๐Ÿงจ Other Ways to Truncate Numbers in JavaScript (Use with Caution)

Sometimes you want to truncate a number without using Math.trunc(). Here are some alternatives - each with its own quirks.

๐Ÿ”ธ Bitwise OR (| 0)

This is a clever trick to truncate decimals using bitwise operations.

4.9 | 0; // 4 ~~4.9; // 4 
Enter fullscreen mode Exit fullscreen mode

โœ… Fast
โš ๏ธ Only works reliably for 32-bit integers. Avoid with large or precise numbers.


๐Ÿ”ธ Double Bitwise NOT (~~)

Similar to | 0, but slightly more readable for some developers.

~~4.9; // 4 ~~-4.9; // -4 
Enter fullscreen mode Exit fullscreen mode

โœ… Fast
โš ๏ธ Same limitations as | 0.


๐Ÿ”ธ Math.floor() and Math.ceil()

These are rounding functions, not true truncation - but they can be useful depending on the sign of the number.

Math.floor(4.9); // 4 Math.floor(-4.9); // -5 Math.ceil(4.9); // 5 Math.ceil(-4.9); // -4 
Enter fullscreen mode Exit fullscreen mode

โœ… Clear intent
โš ๏ธ Not truncation - they round up/down.


๐Ÿ”ธ Number.toFixed(0)

This method returns a string, not a number, and it rounds the value.

(4.9).toFixed(0); // "5" Number((4.9).toFixed(0)); // 5 
Enter fullscreen mode Exit fullscreen mode

โœ… Useful for formatting
โš ๏ธ Not truncation, and returns a string unless converted.


๐Ÿ“Š Recommendation

Method Truncates? Returns Performance Notes
Math.trunc() โœ… Number ๐Ÿ”ผ High Best choice for clarity and safety
parseInt() โœ… Number ๐Ÿ”ฝ Low Avoid for truncating numbers
Math.floor() โŒ Number ๐Ÿ”ผ High Rounds down, not truncates
Math.ceil() โŒ Number ๐Ÿ”ผ High Rounds up, not truncates
toFixed(0) โŒ String ๐Ÿ”ฝ Medium Rounds and returns string unless converted

โœ… Why Best Practices Matter

As a developer, you're not just writing for machines - you're writing for other humans, including your future self. Choosing the right tool makes your code:

  • Easier to read
  • Faster to run
  • More meaningful

๐Ÿ’ฌ Final Thoughts

If you're truncating numbers with parseInt, stop now. Use Math.trunc and write code that makes sense. Small choices lead to better habits - and better software.


Enjoyed this tip? Share it with a friend who's still stuck in parseInt land. Let's do the basics right - then we can do the magic. ๐Ÿ˜‰

Top comments (2)

Collapse
 
dev_king profile image
Dev King

Very nice

Collapse
 
werliton profile image
Werliton Silva

I am glad hear this