Skip to content
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { DOMParser } = require('@xmldom/xmldom');
const xpath = require('./xpath');

const xml = `<root xmlns:ns1="http://example.com/ns1" xmlns:ns2="http://example.com/ns2" xmlns:ns3="http://example.com/ns3" xmlns:ns4="http://example.com/ns4" xmlns:ns5="http://example.com/ns5" ID="id12345" Version="1.0">
<ns1:Item1>
<ns1:SubItem1>Value1</ns1:SubItem1>
<ns1:SubItem2>Value2</ns1:SubItem2>
</ns1:Item1>
<ns2:Item2>
<ns2:SubItem1>Value3</ns2:SubItem1>
<ns2:SubItem2>Value4</ns2:SubItem2>
</ns2:Item2>
<ns3:Item3>
<ns3:SubItem1Target>Value5</ns3:SubItem1Target>
<ns3:SubItem2>Value6</ns3:SubItem2>
</ns3:Item3>
<ns4:Item4>
<ns4:SubItem1>Value7</ns4:SubItem1>
<ns4:SubItem2>Value8</ns4:SubItem2>
</ns4:Item4>
<ns5:Item5>
<ns5:SubItem1>Value9</ns5:SubItem1>
<ns5:SubItem2>Value10</ns5:SubItem2>
</ns5:Item5>
</root>`;

const doc = new DOMParser().parseFromString(xml);
const ITERATIONS = 100000;

const start = Date.now();
for (let i = 0; i < ITERATIONS; i++) {
xpath.select("//*[local-name()='SubItem1Target']", doc);
}
const end = Date.now();

console.log(`Benchmark completed in ${(end - start).toFixed(2)} milliseconds for ${ITERATIONS} iterations.`
);
19 changes: 14 additions & 5 deletions xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
return nodes;
}

var ctx = c.extend({});
var ctx = c.clone();

return reduce(
function (inNodes, pred) {
Expand Down Expand Up @@ -2199,7 +2199,7 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
};

PathExpr.prototype.evaluate = function (c) {
var xpc = assign(new XPathContext(), c);
var xpc = c.clone();

var filterResult = this.applyFilter(c, xpc);

Expand Down Expand Up @@ -3520,9 +3520,18 @@ var xpath = (typeof exports === 'undefined') ? {} : exports;
this.functionResolver = fr != null ? fr : new FunctionResolver();
}

XPathContext.prototype.extend = function (newProps) {
return assign(new XPathContext(), this, newProps);
};
XPathContext.prototype.clone = function () {
var copy = new XPathContext(this.variableResolver, this.namespaceResolver, this.functionResolver);
copy.expressionContextNode = this.expressionContextNode;
copy.virtualRoot = this.virtualRoot;
copy.caseInsensitive = this.caseInsensitive;
copy.contextNode = this.contextNode;
copy.contextSize = this.contextSize;
copy.contextPosition = this.contextPosition;
copy.isHtml = this.isHtml;
copy.allowAnyNamespaceForNoPrefix = this.allowAnyNamespaceForNoPrefix;
return copy
}

// VariableResolver //////////////////////////////////////////////////////////

Expand Down