Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified code #2

Merged
merged 1 commit into from Mar 23, 2016
Merged

Simplified code #2

merged 1 commit into from Mar 23, 2016

Conversation

fearphage
Copy link
Contributor

Used the native functionality instead of external modules. This greatly improved performance as a side effect:

➜  is-positive-integer git:(master) node benchmark.js
original x 1,749,034 ops/sec ±3.87% (87 runs sampled)
bitshift x 56,364,251 ops/sec ±1.65% (88 runs sampled)
Math.floor x 15,037,634 ops/sec ±5.51% (74 runs sampled)
parseInt x 13,990,961 ops/sec ±5.95% (76 runs sampled)
Fastest is bitshift

It's roughly 25 times faster now.

Source: https://gist.github.com/fearphage/3d50dd7a454a37e35392

improved performance as a side effect
@fr33kvanderwand
Copy link

why not simply return x > 0, ?

@f
Copy link

f commented Mar 23, 2016

@fr33kvanderwand Because 1.2 is > 0 but not Integer but floating number.

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

Nice, passes all tests. Can you explain how this works exactly? Thanks 👍

@tjmehta tjmehta mentioned this pull request Mar 23, 2016
@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

Going to pull this in, but please do provide me with an explanation. I would love to add it in as comments. Thanks!

@tjmehta tjmehta merged commit 6551ee2 into tjmehta:master Mar 23, 2016
@cameroncooper
Copy link

This function returns false for Number.MAX_SAFE_INTEGER

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

@fearphage, thoughts on #5 ? EDIT: #6

@cameroncooper
Copy link

It's actually #6 I made an odd mistake pulling in the changes from #2 .I personally think #2 is neat though bitshifting in JS makes me nervous mostly for reasons like this.

@fearphage
Copy link
Contributor Author

Nice, passes all tests.

It also adheres to your lint rules.

>> is a bitwise operator. More importantly it's the sign-propagating right shift operator. It says shift the number this many bits (0 in this case) and maintain the sign (positive or negative). Bit shifting only applies to integers. So bit shifting any non-integer is roughly equivalent to calling Math.floor(x) or parseInt(x, 10) or x | 0 (another bit shift operator).

This function returns false for Number.MAX_SAFE_INTEGER

I've read that MAX_SAFE_INTEGER is the largest number that can be expressed in 53 bits (53^2 - 1). It's an integer written as a double float. Bit shifting only works with integers and in the case of >> signed integers. MAX_SAFE_INTEGER doesn't have space for a sign. That's why MIN_SAFE_INTEGER exists. Going above this number gives strange results:

Math.pow(2, 53) === Math.pow(2, 53) + 1 // returns true

You could add || x === Number.MAX_SAFE_INTEGER to resolve this shortcoming.

@fearphage fearphage deleted the simplification branch March 23, 2016 20:22
@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

Added a bunch of broken test cases here: https://github.com/tjmehta/is-positive-integer/pull/7/files

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

Looks like it breaks for more than just the "MAX" cases

@mitsuhiko
Copy link

Not sure if anyone cares, but from looking at the code the behavior was changed with regards to what happens if you pass non numeric objects in that define valueOf.

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

@mitsuhiko, which non numeric objects functionality changed? Is there a missing test case: https://github.com/tjmehta/is-positive-integer/blob/master/test/test.js?

@mitsuhiko
Copy link

@tjmehta for instance an object like this:

let foo = {
  valueOf: function() { return 42; }
};

In the old code this was not considered an integer, in this code I assume it will be one due to the coercion. I did not actually test any of this. Just saw the PR pop up on twitter.

@mitsuhiko
Copy link

Also pretty sure passing in true will have similar problems as it coerces into 1 in some contexts.

@twistor
Copy link

twistor commented Mar 23, 2016

Why not?

Number.isInteger(x) && x > 0

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

@twistor, Number.isInteger is es6

@tjmehta
Copy link
Owner

tjmehta commented Mar 23, 2016

@mitsuhiko, just tested, both are false. added test cases: https://github.com/tjmehta/is-positive-integer/pull/7/files

@mitsuhiko
Copy link

@tjmehta indeed. the === x check should catch those cases.

I have nothing further to add :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants