[Pkg-javascript-commits] [ltx] 425/469: better benchmarks

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:34 UTC 2016


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

js pushed a commit to branch master
in repository ltx.

commit 860de9f9800ec31d809529d6856d200064636ab8
Author: Sonny Piers <sonny at fastmail.net>
Date:   Fri Jan 29 19:39:24 2016 +0100

    better benchmarks
---
 .npmignore          |  2 +-
 README.md           | 15 +++++-----
 benchmarks/index.js | 20 ++++++++++++++
 benchmarks/ltx.js   | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 benchmarks/parse.js | 16 +++++------
 benchmarks/write.js | 16 +++++------
 package.json        |  5 ++--
 7 files changed, 124 insertions(+), 29 deletions(-)

diff --git a/.npmignore b/.npmignore
index 940389c..777c99e 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,3 +1,4 @@
+benchmarks/
 test/
 
 .editorconfig
@@ -5,5 +6,4 @@ test/
 .npmignore
 .travis.yml
 
-benchmark.js
 CONTRIBUTING.md
diff --git a/README.md b/README.md
index dc95719..584089f 100644
--- a/README.md
+++ b/README.md
@@ -7,12 +7,12 @@
 
 ltx is a fast XML builder, parser and manipulation library for JavaScript.
 
-The builder is a convenient and succint API to build XML documents represented in memory as JavaScript primitives that can be serialized to XML strings. It provides a [JSX](https://facebook.github.io/jsx/) compatible API as well.
+The builder is a convenient and succinct API to build XML documents represented in memory as JavaScript primitives that can be serialized to XML strings. It provides a [JSX](https://facebook.github.io/jsx/) compatible API as well.
 
-The parser can parse XML documents or streams and support diffrent backends.
+The parser can parse XML documents or streams and support different backends.
 
 Features:
-* Succint API to build and manipulate XML objects
+* Succinct API to build and manipulate XML objects
 * parse XML strings
 * parse XML streams
 * multiple parser backends
@@ -20,8 +20,9 @@ Features:
   * [node-xml](https://github.com/dylang/node-xml)
   * [node-expat](https://github.com/node-xmpp/node-expat)
   * [libxmljs](https://github.com/polotek/libxmljs)
-  * [ltx](https://github.com/node-xmpp/ltx/blob/master/lib/parsers/ltx.js) (default)
-* [JSX](https://facebook.github.io/jsx/) compatible
+  * [ltx](https://github.com/node-xmpp/ltx/blob/master/lib/parsers/ltx.js) (default and fastest see [Benchmarks](#benchmarks))
+* [JSX](https://facebook.github.io/jsx/) compatible (use `ltx.createElement` pragma)
+* [tagged template](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) support `` ltx`<foo bar="${baz}">` ``
 
 ## Install
 
@@ -34,8 +35,7 @@ For documentation please see http://node-xmpp.org/doc/ltx.html
 ## Benchmark
 
 ```
-node benchmarks/parse.js
-node benchmarks/write.js
+npm run benchmark
 ```
 
 ## Test
@@ -48,4 +48,3 @@ npm test
 ## Licence
 
 MIT
-
diff --git a/benchmarks/index.js b/benchmarks/index.js
new file mode 100644
index 0000000..9d8b85a
--- /dev/null
+++ b/benchmarks/index.js
@@ -0,0 +1,20 @@
+'use strict'
+
+var readdir = require('fs').readdirSync
+var basename = require('path').basename
+
+readdir(__dirname).forEach(function (file) {
+  if (file === basename(__filename)) return
+
+  var suite = require('./' + file)
+  console.log('suite', suite.name)
+  suite
+  .on('cycle', function (event) {
+    console.log(event.target.toString())
+  })
+  .on('complete', function () {
+    console.log('Fastest is ' + this.filter('fastest').map('name'))
+  })
+  .run({'async': false})
+  console.log('\n')
+})
diff --git a/benchmarks/ltx.js b/benchmarks/ltx.js
new file mode 100644
index 0000000..b0c436e
--- /dev/null
+++ b/benchmarks/ltx.js
@@ -0,0 +1,79 @@
+'use strict'
+
+/*
+  benchmark the speed of the different methods to create elements
+  Not all tests are equally functional but it gives a good idea of what to expect from
+  the different techniques.
+ */
+
+var benchmark = require('benchmark')
+var ltx = require('../index')
+var createElement = ltx.createElement
+var tag = ltx.tag
+var Element = ltx.Element
+var parse = ltx.parse
+
+var XML = [
+  '<message to="foo at bar" from="bar at foo" type="chat" id="foobar">',
+  '<body>Where there is love there is life.</body>',
+  '</message>'
+].join('')
+var el = parse(XML)
+
+var suite = new benchmark.Suite('ltx')
+
+suite.add('tag with template literal', function () {
+  tag`
+    <message to="${'foo at bar'}" from="${'bar at foo'}" type="${'chat'}" id="${'foobar'}">
+      <body>${'Where there is love there is life.'}</body>
+    </message>
+  `
+})
+
+suite.add('tag with direct call', function () {
+  tag(
+    [
+      '\n  <message to="',
+      '" from="',
+      '" type="',
+      '" id="',
+      '">\n    <body>',
+      '</body>\n  </message>\n'
+    ],
+    'foo at bar',
+    'bar at foo',
+    'chat',
+    'foobar',
+    'Where there is love there is life.'
+  )
+})
+
+suite.add('serialize and parse', function () {
+  parse(el.toString())
+})
+
+suite.add('parse', function () {
+  parse(XML)
+})
+
+suite.add('serialize', function () {
+  el.toString()
+})
+
+suite.add('createElement (jsx)', function () {
+  createElement(
+    'message', {to: 'foo at bar', from: 'bar at foo', type: 'chat', id: 'foobar'},
+    createElement('body', null, 'Where there is love there is life.')
+  )
+})
+
+suite.add('clone', function () {
+  el.clone()
+})
+
+suite.add('Element', function () {
+  new Element('message', {to: 'foo at bar', from: 'bar at foo', type: 'chat', id: 'foobar'})
+    .c('body').t('Where there is love there is life.').root()
+})
+
+module.exports = suite
diff --git a/benchmarks/parse.js b/benchmarks/parse.js
index b9f1828..5dfa6e5 100644
--- a/benchmarks/parse.js
+++ b/benchmarks/parse.js
@@ -1,5 +1,9 @@
 'use strict'
 
+/*
+  benchmark the parsing speed of the supported backends
+ */
+
 var benchmark = require('benchmark')
 var ltx = require('../index')
 var parsers = require('../lib/parsers')
@@ -10,18 +14,12 @@ var XML = [
   '</message>'
 ].join('')
 
-var suite = new benchmark.Suite('parse')
+var suite = new benchmark.Suite('backends parse')
 
 parsers.forEach(function (Parser) {
-  suite.add(Parser.name, function () {
+  suite.add(Parser.name.slice(3), function () {
     ltx.parse(XML, {Parser: Parser})
   })
 })
 
-suite.on('cycle', function (event) {
-  console.log(event.target.toString())
-})
-  .on('complete', function () {
-    console.log('Fastest is ' + this.filter('fastest').map('name'))
-  })
-  .run({'async': true})
+module.exports = suite
diff --git a/benchmarks/write.js b/benchmarks/write.js
index 30c6a2c..c93cad7 100644
--- a/benchmarks/write.js
+++ b/benchmarks/write.js
@@ -1,5 +1,9 @@
 'use strict'
 
+/*
+  benchmark the serialization speed of the the supported backends
+ */
+
 var benchmark = require('benchmark')
 var parsers = require('../lib/parsers')
 
@@ -9,20 +13,14 @@ var XML = [
   '</message>'
 ].join('')
 
-var suite = new benchmark.Suite('write')
+var suite = new benchmark.Suite('backends write')
 
 parsers.forEach(function (Parser) {
   var parser = new Parser()
   parser.write('<r>')
-  suite.add(Parser.name, function () {
+  suite.add(Parser.name.slice(3), function () {
     parser.write(XML)
   })
 })
 
-suite.on('cycle', function (event) {
-  console.log(event.target.toString())
-})
-  .on('complete', function () {
-    console.log('Fastest is ' + this.filter('fastest').map('name'))
-  })
-  .run({'async': true})
+module.exports = suite
diff --git a/package.json b/package.json
index 991db89..93bf139 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
   "scripts": {
     "prepublish": "npm run bundle",
     "preversion": "npm test",
+    "benchmark": "node benchmarks/",
     "bundle": "browserify -s ltx index.js -o bundle.js",
     "unit": "vows --spec",
     "lint": "standard",
@@ -40,12 +41,12 @@
     "inherits": "^2.0.1"
   },
   "devDependencies": {
-    "benchmark": "^2.0.0",
+    "benchmark": "^2.1.0",
     "libxmljs": "^0.17.0",
     "microtime": "^2.0.0",
     "node-expat": "^2.3.12",
     "node-xml": "^1.0.2",
-    "sax": "^1.1.4",
+    "sax": "^1.1.5",
     "vows": "^0.8.1"
   }
 }

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



More information about the Pkg-javascript-commits mailing list