Giter Club home page Giter Club logo

Comments (9)

hshoff avatar hshoff commented on May 7, 2024

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.

hshoff avatar hshoff commented on May 7, 2024

Here's a jsPerf Coercion vs. Casting vs. Bitshift

Looks like casting with parseInt() is the fastest.

from javascript.

ajacksified avatar ajacksified commented on May 7, 2024

parseInt looks good.

from javascript.

ssorallen avatar ssorallen commented on May 7, 2024

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.

ssorallen avatar ssorallen commented on May 7, 2024

If we go with parseInt, we should enforce always including a radix.

// Bad
parseInt('123');

// Good
parseInt('123', 10);

from javascript.

ajacksified avatar ajacksified commented on May 7, 2024

Took a look with other browsers- bitshift is drastically better on FF and IE. Not sure if buggy test result.

from javascript.

hshoff avatar hshoff commented on May 7, 2024

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.

clizzin avatar clizzin commented on May 7, 2024

πŸ‘ 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.

reissbaker avatar reissbaker commented on May 7, 2024

πŸ‘ 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.