Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by applying binary search to nodes #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 76 additions & 12 deletions xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,22 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
}
};

function compare(n1, n2) {
if (n1.lineNumber < n2.lineNumber) {
return -1;
}

if (n1.lineNumber > n2.lineNumber) {
return 1;
}

if (n1.columnNumber < n2.columnNumber) {
return -1;
}

return 1;
}

function nodeOrder(n1, n2) {
if (n1 === n2) {
return 0;
Expand Down Expand Up @@ -3056,14 +3072,38 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
if (n1Par) {
var cn = n1isAttr ? n1Par.attributes : n1Par.childNodes,
len = cn.length;
for (var i = 0; i < len; i += 1) {
var n = cn[i];
if (n === n1) {
return -1;
}
if (n === n2) {
return 1;
}

var start = 0;
var end = len - 1;

while (start <= end) {
var mid = Math.floor(start + (end - start)/2);

var midNode = cn[mid];

var fn1 = compare(n1, midNode);
var fn2 = compare(n2, midNode);

if (fn1 == 0) {
return -fn2;
}

if (fn2 == 0) {
return fn1;
}

if (fn1 < 0 && fn2 < 0) {
end = mid - 1;
}
else if (fn1 > 0 && fn2 > 0) {
start = mid + 1;
}
else if (fn1 < 0) {
return -1;
}
else {
return 1;
}
}
}

Expand Down Expand Up @@ -3211,11 +3251,35 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
return p.node;
};

XNodeSet.prototype.contains = function(n) {
if (this.nodes.length == 0) {
return false;
}

var start = 0;
var end = this.nodes.length - 1;

while (start <= end) {
var mid = Math.floor(start + (end - start)/2);

var midNode = this.nodes[mid];

if (n === midNode) {
return true;
}

if (compare(n, midNode) < 0) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
};

XNodeSet.prototype.add = function (n) {
for (var i = 0; i < this.nodes.length; i += 1) {
if (n === this.nodes[i]) {
return;
}
if (this.contains(n)) {
return;
}

this.tree = null;
Expand Down