Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Fix "-1" array accesses in CommentsParser. #777

Merged
merged 1 commit into from
Oct 28, 2017

Conversation

bmeurer
Copy link
Member

@bmeurer bmeurer commented Oct 28, 2017

Similar to the fixes in babel/babel#6580 and
babel/babel#6581, accesses of the form

stack[stack.length - 1];

when stack can be an empty array are pretty bad for performance.
In this case it also breaks the type safety, since the function
last<T> is declared to only return values of type T, but
occasionally also returns undefined now, since the stack parameters
passed to it never contain a property "-1" and neither do the
Object.prototype or the Array.prototype.

This is a non-breaking performance fix, which adds proper checking
to ensure that last is only invoked on non-empty arrays.

Similar to the fixes in babel/babel#6580 and
babel/babel#6581, accesses of the form

```js
stack[stack.length - 1];
```

when `stack` can be an empty array are pretty bad for performance.
In this case it also breaks the type safety, since the function
`last<T>` is declared to only return values of type `T`, but
occasionally also returns `undefined` now, since the `stack` parameters
passed to it never contain a property `"-1"` and neither do the
`Object.prototype` or the `Array.prototype`.

This is a non-breaking performance fix, which adds proper checking
to ensure that `last` is only invoked on non-empty arrays.
@hzoo hzoo merged commit 55d5545 into babel:master Oct 28, 2017
@hzoo
Copy link
Member

hzoo commented Oct 28, 2017

Nice, thanks @bmeurer! (and turning off comments in the parser saves a lot as well 😂), since honestly just need the license comments for Babel and should drop everything else

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

Successfully merging this pull request may close these issues.

2 participants