Fix path.popContext() to not try to load "-1" from contexts array. (#6580)

* Fix path.popContext() to not try to load "-1" from contexts array.

The current implement of popContext does

```js
this.setContext(this.contexts[this.contexts.length - 1]);
```

even if `this.contexts` can be empty, which causes it to lookup the
property `"-1"`, which is not found on the array itself and obviously
also not in the `Object.prototype` and the `Array.prototype`. However
since `"-1"` is not a valid array index, but has a valid integer
representation, this is a very expensive lookup in V8 (and probably
other engines too, but that is probably less relevant, since Babel
most often runs on Node nowadays).

* Make zero check explicit (for readability).
This commit is contained in:
Benedikt Meurer 2017-10-28 22:17:05 +02:00 committed by Henry Zhu
parent df0d9d05a3
commit f9e0643460

View File

@ -194,7 +194,11 @@ export function _resyncRemoved() {
export function popContext() {
this.contexts.pop();
this.setContext(this.contexts[this.contexts.length - 1]);
if (this.contexts.length > 0) {
this.setContext(this.contexts[this.contexts.length - 1]);
} else {
this.setContext(undefined);
}
}
export function pushContext(context) {