[Pkg-javascript-commits] [node-detective] 79/119: using acorn, faster from 700ms to 560ms to parse jquery

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:44:37 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-detective.

commit cb2148a349eb025d4f50f5ceb469aa2f6e0c59e8
Author: James Halliday <mail at substack.net>
Date:   Wed Nov 19 12:31:02 2014 +0100

    using acorn, faster from 700ms to 560ms to parse jquery
---
 .gitignore                |  1 +
 .npmignore                |  1 +
 bench/detect.js           |  7 +++++++
 bench/esprima_v_acorn.txt | 18 ++++++++++++++++++
 index.js                  | 20 ++++++++++++++++++--
 package.json              |  1 +
 test/files/yield.js       |  6 ++++--
 7 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3c3629e..2f9fd0e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 node_modules
+bench/src/jquery.js
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..6ca4de8
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+bench/src/jquery.js
diff --git a/bench/detect.js b/bench/detect.js
new file mode 100644
index 0000000..b7d0dd9
--- /dev/null
+++ b/bench/detect.js
@@ -0,0 +1,7 @@
+var detective = require('../');
+var fs = require('fs');
+
+var src = fs.readFileSync(__dirname + '/src/jquery.js', 'utf8');
+var t0 = Date.now();
+var requires = detective(src);
+console.log(Date.now() - t0);
diff --git a/bench/esprima_v_acorn.txt b/bench/esprima_v_acorn.txt
new file mode 100644
index 0000000..a2340fe
--- /dev/null
+++ b/bench/esprima_v_acorn.txt
@@ -0,0 +1,18 @@
+esprima:
+
+  $ for i in {1..5}; do node detect.js; done
+  704
+  702
+  704
+  704
+  697
+
+acorn:
+
+  $ for i in {1..5}; do node detect.js; done
+  555
+  552
+  585
+  549
+  583
+
diff --git a/index.js b/index.js
index e474814..d8abb7d 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,20 @@
-var esprima = require('esprima-fb');
+var aparse = require('acorn').parse;
+var defined = require('defined');
+
+function parse (src, opts) {
+    if (!opts) opts = {};
+    return aparse(src, {
+        ecmaVersion: defined(opts.ecmaVersion, 6),
+        ranges: defined(opts.ranges, opts.range),
+        locations: defined(opts.locations, opts.loc),
+        allowReturnOutsideFunction: defined(
+            opts.allowReturnOutsideFunction, true
+        ),
+        strictSemicolons: defined(opts.strictSemicolons, false),
+        allowTrailingCommas: defined(opts.allowTrailingCommas, true),
+        forbidReserved: defined(opts.forbidReserved, false)
+    });
+}
 var escodegen = require('escodegen');
 
 var traverse = function (node, cb) {
@@ -22,7 +38,7 @@ var traverse = function (node, cb) {
 };
 
 var walk = function (src, opts, cb) {
-    var ast = esprima.parse(src, opts);
+    var ast = parse(src, opts);
     traverse(ast, cb);
 };
 
diff --git a/package.json b/package.json
index 05dd0a6..5eacfaa 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
     "test": "tap test/*.js"
   },
   "dependencies": {
+    "defined": "0.0.0",
     "escodegen": "~1.1.0",
     "esprima-fb": "4001.1.0-dev-harmony-fb"
   },
diff --git a/test/files/yield.js b/test/files/yield.js
index 8f4ac5e..36bd2b9 100644
--- a/test/files/yield.js
+++ b/test/files/yield.js
@@ -1,2 +1,4 @@
-var a = require('a');
-var b = yield require('c')(a);
+(function * () {
+    var a = require('a');
+    var b = yield require('c')(a);
+})();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-detective.git



More information about the Pkg-javascript-commits mailing list