Permalink
Browse files

Initial commit.

  • Loading branch information...
kitcambridge committed Feb 13, 2012
0 parents commit 8401dce1d203621279d8086f5e4c6963ee7f2c7d
Showing with 5,019 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +64 −0 benchmark/benchmark.js
  3. +64 −0 benchmark/update.js
  4. +3,284 −0 benchmark/vendor/benchmark.js
  5. +390 −0 lib/prim.js
  6. +30 −0 package.json
  7. +168 −0 test/test_prim.js
  8. +999 −0 test/vendor/underscore.js
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Kit Cambridge.
+http://kitcambridge.github.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
@@ -0,0 +1,64 @@
+/*
+ * Prim benchmark suite.
+ * Requires Node.
+*/
+
+var prim = require("../lib/prim").parse, Benchmark = require("./vendor/benchmark"),
+
+// Load the various JSON implementations.
+// --------------------------------------
+
+// Crockford's recursive descent parser.
+json_parse = require("./vendor/json_parse"),
+
+// Crockford's state machine parser.
+json_parse_state = require("./vendor/json_parse_state"),
+
+// Bozhilov's JSON parser.
+evalJSON = require("./vendor/json"),
+
+// Samuel's `json-sans-eval`.
+jsonParse = require("./vendor/json_sans_eval"),
+
+suite = new Benchmark.Suite("Prim Benchmark Suite");
+
+suite.add("Prim", function () {
+ prim('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+suite.add("Crockford's recursive descent parser", function () {
+ json_parse('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+suite.add("Crockford's state machine parser", function () {
+ json_parse_state('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+suite.add("Bozhilov's JSON parser", function () {
+ evalJSON('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+suite.add("Samuel's `json-sans-eval`", function () {
+ jsonParse('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+suite.add("Native `JSON.parse`", function () {
+ JSON.parse('{"a": [1, 2, {"b": 3, "c": 4}], "d": 123, "e": "hello"}');
+});
+
+// Register event handlers for logging results.
+suite.on("cycle", function (event, results) {
+ console.log(String(results));
+}).on("complete", function () {
+ var results = this.filter("successful"), fastest = results.filter("fastest"), slowest = results.filter("slowest");
+ results.forEach(function (result) {
+ var percent, hz = result.hz, text;
+ if (fastest.indexOf(result) > -1) {
+ console.log("Fastest: `%s`.", result.name);
+ } else if (slowest.indexOf(result) > -1) {
+ console.log("Slowest: `%s`.%s", result.name, isFinite(hz) ? " " + Math.round((1 - hz / fastest[0].hz) * 100) + "% slower." : "");
+ }
+ });
+});
+
+suite.run({ "async": true });
@@ -0,0 +1,64 @@
+/*
+ * Downloads and wraps various JSON implementations for benchmarking.
+ * Requires Node.
+*/
+
+var https = require("https"), url = require("url"), path = require("path"), fs = require("fs");
+
+// Fetches a source file.
+function get(source, callback) {
+ source = url.parse(source);
+ https.get(source, function (response) {
+ var data = "";
+ response.setEncoding("utf-8");
+ response.on("data", function (chunk) {
+ data += chunk;
+ }).on("end", function () {
+ callback(null, path.basename(source.path), data);
+ });
+ }).on("error", callback);
+}
+
+// Creates a CommonJS module wrapper around a JSON implementation. Returns a
+// callback function that can be passed to the `get` function.
+function wrap(exports, callback) {
+ return function (exception, name, data) {
+ if (exception) {
+ callback(exception);
+ } else {
+ fs.writeFile("vendor/" + name, data + ";" + exports + ";", callback);
+ }
+ };
+}
+
+// Douglas Crockford's recursive descent JSON parser.
+get("https://raw.github.com/douglascrockford/JSON-js/master/json_parse.js", wrap("module.exports = json_parse", function (exception) {
+ if (exception) {
+ throw exception;
+ }
+ console.log("Downloaded `json_parse` successfully.");
+}));
+
+// Douglas Crockford's state machine parser.
+get("https://raw.github.com/douglascrockford/JSON-js/master/json_parse_state.js", wrap("module.exports = json_parse", function (exception) {
+ if (exception) {
+ throw exception;
+ }
+ console.log("Downloaded `json_parser_state` successfully.");
+}));
+
+// Mike Samuel's `json-sans-eval`.
+get("https://json-sans-eval.googlecode.com/svn/trunk/src/json_sans_eval.js", wrap("module.exports = jsonParse", function (exception) {
+ if (exception) {
+ throw exception;
+ }
+ console.log("Downloaded `json-sans-eval` successfully.");
+}));
+
+// Asen Bozhilov's JSON parser.
+get("https://raw.github.com/abozhilov/json/master/src/json.js", wrap("module.exports = evalJSON", function (exception) {
+ if (exception) {
+ throw exception;
+ }
+ console.log("Downloaded `evalJSON` successfully.");
+}));
Oops, something went wrong.

0 comments on commit 8401dce

Please sign in to comment.