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

Why is the position parameter in the arrayIncludes method using | instead of || ? #114

Closed
dorisentomari opened this issue Nov 11, 2019 · 3 comments

Comments

@dorisentomari
Copy link

  1. line 1

const lookupIndex = position | 0

  1. line 2

return str.indexOf(searchString, position || 0) !== -1

so, I cannot understand, why you position | 0 in line 1, and use position || 0 on line 2? I thinking both of them could use position || 0 is corrent.

@lazarljubenovic
Copy link

Not sure if this was the intention, but | 0 is a quick way to cast to integer, || 0 is a quick way to make a falsy value into zero.

@dorisentomari
Copy link
Author

Not sure if this was the intention, but | 0 is a quick way to cast to integer, || 0 is a quick way to make a falsy value into zero.

Thank you reply me, at first I thought about same with you, but I wonder if it's necessary to think about float number, and the stringIncludes function doesn't think about this, so I think we need not think about float number here.

But, if we use | 0 to cast to int , it's no problem. Maybe I think about it too detail.

Tips: lexer.js's code is too hard, I need more time to learn it. 😂😂😂

@andrejewski
Copy link
Owner

@Dawnight, @lazarljubenovic is correct about the | 0. This also, back in the day, was a means to say a value was an integer such that it could be validated at compile time for asm.js.

When I added these compat methods, I wanted to be sure that they had the same behavior as the native methods. Looking at the usages of arrayIncludes we don't use position anywhere, nor do we need the explicit NaN handling, so that could be cleaned up. However, these days if we were looking to squeeze out performance, we'd use Array#includes and friends directly.

The lexer module isn't as hard to read/understand as parser in my experiences. Both are complicated, which is mostly combated by having an exhaustive test suite.

Closing as the question has been answered; thanks @lazarljubenovic!

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

No branches or pull requests

3 participants