Skip to content

Commit

Permalink
improve DOM walking performances on the 'base' and 'noqsa' versions b…
Browse files Browse the repository at this point in the history
…y using the native traversal API were available
  • Loading branch information
Diego committed Jul 24, 2017
1 parent 0daa432 commit 413d17c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/nwmatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
'text': 1, 'type': 1, 'valign': 1, 'valuetype': 1, 'vlink': 1
},

NATIVE_TRAVERSAL_API =
'nextElementSibling' in root &&
'previousElementSibling' in root,

Selectors = { },

Operators = {
Expand Down Expand Up @@ -512,11 +516,15 @@
}

else if ((match = selector.match(Patterns.adjacent))) {
source = 'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + 'break;}}e=N' + k + ';';
source = NATIVE_TRAVERSAL_API ?
'var N' + k + '=e;if((e=e.previousElementSibling)){' + source + '}e=N' + k + ';' :
'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + 'break;}}e=N' + k + ';';
}

else if ((match = selector.match(Patterns.relative))) {
source = 'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + '}}e=N' + k + ';';
source = NATIVE_TRAVERSAL_API ?
'var N' + k + '=e;while((e=e.previousElementSibling)){' + source + '}e=N' + k + ';' :
'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + '}}e=N' + k + ';';
}

else if ((match = selector.match(Patterns.children))) {
Expand Down
12 changes: 10 additions & 2 deletions src/nwmatcher-noqsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
'text': 1, 'type': 1, 'valign': 1, 'valuetype': 1, 'vlink': 1
},

NATIVE_TRAVERSAL_API =
'nextElementSibling' in root &&
'previousElementSibling' in root,

Selectors = { },

Operators = {
Expand Down Expand Up @@ -562,11 +566,15 @@
}

else if ((match = selector.match(Patterns.adjacent))) {
source = 'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + 'break;}}e=N' + k + ';';
source = NATIVE_TRAVERSAL_API ?
'var N' + k + '=e;if((e=e.previousElementSibling)){' + source + '}e=N' + k + ';' :
'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + 'break;}}e=N' + k + ';';
}

else if ((match = selector.match(Patterns.relative))) {
source = 'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + '}}e=N' + k + ';';
source = NATIVE_TRAVERSAL_API ?
'var N' + k + '=e;while((e=e.previousElementSibling)){' + source + '}e=N' + k + ';' :
'var N' + k + '=e;while((e=e.previousSibling)){if(e.nodeType==1){' + source + '}}e=N' + k + ';';
}

else if ((match = selector.match(Patterns.children))) {
Expand Down

0 comments on commit 413d17c

Please sign in to comment.