Comments (9)
I always do +'15'
, open to changing this if there's a more preferred way.
see Numbers: https://github.com/airbnb/javascript#type-coercion
from javascript.
Here's a jsPerf Coercion vs. Casting vs. Bitshift
Looks like casting with parseInt()
is the fastest.
from javascript.
parseInt looks good.
from javascript.
I didn't expect bitshifting to be so much slower than parseInt
.
I always use parseInt
because it's explicit about what you're doing. +'1'
and '1' >> 0
work, but they're tricks. If you are parsing an integer, you can't get more explicit than a function called "parseInt".
from javascript.
If we go with parseInt
, we should enforce always including a radix.
// Bad
parseInt('123');
// Good
parseInt('123', 10);
from javascript.
Took a look with other browsers- bitshift is drastically better on FF and IE. Not sure if buggy test result.
from javascript.
Woah, what's Webkit got against bitshift?
I vote we go with parseInt
because it's explicit.
But it should be allowed to do bitshifting if you need to performance boost on lots of strings to ints. Maybe just leave a comment why you think you need to bitshift over being explicit?
from javascript.
π for parseInt
in general for clarity, and bitshifting with an explanatory comment when necessary for performance βΒ although IMO it takes a lot to justify performance > clarity. But that's just bikeshedding.
Also π for including a radix when calling parseInt
. It is ugly and verbose, but it's a hell of a lot better than a prefixed 0
ruining your day.
from javascript.
π for parseInt
with a radix. Bitshifts aren't very clear, and for our types of applications VM execution speed is a non-concern.
But it should be allowed to do bitshifting if you need to performance boost on lots of strings to ints.
Fair, if you have profiling that proves parseInt
is a major performance bottleneck. But optimizing without profiling is a bad idea.
from javascript.
Related Issues (20)
- [spam] HOT 1
- [spam]
- [spam] HOT 1
- Multiline comments HOT 3
- [spam]
- [spam]
- TypeError: Failed to load plugin 'jsx-a11y' declared HOT 4
- [spam]
- [spam] HOT 1
- Regarding Updation HOT 4
- [spam] HOT 1
- [spam]
- Link 404 not found HOT 2
- Can we add on quiz sections on this repo too HOT 11
- [spam]
- eslint v9 support
- [spam]
- import/no-extraneous-dependencies shouldn't be a code style rule HOT 25
- Parsing error: Unexpected character '#' HOT 3
- [spam] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.