From 27ec5ed9b16b5fa6fa759d63eb8daea15f037639 Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Tue, 16 Jan 2018 02:57:12 +0000 Subject: [PATCH] Ensure we are nearly always in fast mode on V8 See: https://stackoverflow.com/a/24989927/1518408 --- lib/compiler/visitor.js | 3 ++- lib/util/objects.js | 21 +++++++++++++++++++++ src/parser.pegjs | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/compiler/visitor.js b/lib/compiler/visitor.js index 44d5214..af44280 100644 --- a/lib/compiler/visitor.js +++ b/lib/compiler/visitor.js @@ -18,8 +18,9 @@ class ASTVisitor { // Simple AST node visitor builder for PEG.js static build( functions ) { - const visitor = new ASTVisitor(); + let visitor = new ASTVisitor(); util.extend( visitor, functions ); + visitor = util.enforceFastProperties( visitor ); return visitor.visit.bind( visitor ); } diff --git a/lib/util/objects.js b/lib/util/objects.js index 0cf8339..eddd5f1 100644 --- a/lib/util/objects.js +++ b/lib/util/objects.js @@ -109,6 +109,27 @@ const objects = { }, + // Will ensure on V8 that that property access is always in fast mode + // See: https://stackoverflow.com/a/24989927/1518408 + enforceFastProperties( o ) { + + /* eslint no-unreachable: 0 */ + + function Sub() { } + Sub.prototype = o; + const receiver = new Sub(); + function ic() { + + return typeof receiver.foo; + + } + ic(); + ic(); + return o; + eval( "o" + o ); + + }, + }; module.exports = objects; diff --git a/src/parser.pegjs b/src/parser.pegjs index 3d9957c..895ae7b 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -93,7 +93,7 @@ function createNode( type, details ) { const node = new ast.Node( type, location() ); util.extend( node, details ); - return node; + return util.enforceFastProperties( node ); } }