Permalink
Browse files

Allow suppression of position tracking

  • Loading branch information...
1 parent ff9a74c commit cdafafec0dba1a1feacd9c83c1c2f1672fa6b0dc @isaacs committed Jun 20, 2012
Showing with 18 additions and 10 deletions.
  1. +1 −0 README.md
  2. +17 −10 lib/sax.js
View
@@ -101,6 +101,7 @@ Settings supported:
* `lowercase` - Boolean. If true, then lowercase tag names and attribute names
in loose mode, rather than uppercasing them.
* `xmlns` - Boolean. If true, then namespaces are supported.
+* `position` - Boolean. If false, then don't track line/col/position.
## Methods
View
@@ -68,7 +68,10 @@ function SAXParser (strict, opt) {
if (parser.opt.xmlns) parser.ns = Object.create(rootNS)
// mostly just for error reporting
- parser.position = parser.line = parser.column = 0
+ parser.trackPosition = parser.opt.position !== false
+ if (parser.trackPosition) {
+ parser.position = parser.line = parser.column = 0
+ }
emit(parser, "onready")
}
@@ -341,9 +344,11 @@ function textopts (opt, text) {
function error (parser, er) {
closeText(parser)
- er += "\nLine: "+parser.line+
- "\nColumn: "+parser.column+
- "\nChar: "+parser.c
+ if (parser.trackPosition) {
+ er += "\nLine: "+parser.line+
+ "\nColumn: "+parser.column+
+ "\nChar: "+parser.c
+ }
er = new Error(er)
parser.error = er
emit(parser, "onerror", er)
@@ -589,11 +594,13 @@ function write (chunk) {
if (chunk === null) return end(parser)
var i = 0, c = ""
while (parser.c = c = chunk.charAt(i++)) {
- parser.position ++
- if (c === "\n") {
- parser.line ++
- parser.column = 0
- } else parser.column ++
+ if (parser.trackPosition) {
+ parser.position ++
+ if (c === "\n") {
+ parser.line ++
+ parser.column = 0
+ } else parser.column ++
+ }
switch (parser.state) {
case S.BEGIN:
@@ -612,7 +619,7 @@ function write (chunk) {
var starti = i-1
while (c && c!=="<" && c!=="&") {
c = chunk.charAt(i++)
- if (c) {
+ if (c && parser.trackPosition) {
parser.position ++
if (c === "\n") {
parser.line ++

4 comments on commit cdafafe

Contributor

vvo replied Jun 24, 2012

Hi, what motivated your for adding an option for position tracking? Performance? Thank you

I got three time speed-up after removing line/col stuff. Finally, I switched to easysax. See the issue Sax performance

Contributor

vvo replied Jun 25, 2012

I looked at isaacs/sax-js#75 and https://github.com/vflash/easysax but could not find any unit test coming along with easysax.

I do understand that position tracking cost in term of performance but is it really hard to get best of both worlds: oerformance AND position tracking?

I use sax-js to rewrite some html ans so I use position tracking to copy strings from original html to new html. So I do not need position tracking every time, only on sax events.

I wonder if there's a way to still get position tracking when wanted and an acceptable performance.

BTW what are the JIT/JavaScript reasons for bad performance when tracking position? Thank you.

sax-js is accurate but slow. easysax is fast but badly written and tested. I had to chose speed.

Please sign in to comment.