From f9e06434604f7a090d7802675267359520847f3f Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Sat, 28 Oct 2017 22:17:05 +0200 Subject: [PATCH] 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). --- packages/babel-traverse/src/path/context.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/babel-traverse/src/path/context.js b/packages/babel-traverse/src/path/context.js index 108f0934da..31aef5fdcd 100644 --- a/packages/babel-traverse/src/path/context.js +++ b/packages/babel-traverse/src/path/context.js @@ -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) {