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 ); } }