Skip to content

Commit

Permalink
bdb418305b39dbee11fc5a78b84413b7a3350dcf Dev: Remove dev test from ex…
Browse files Browse the repository at this point in the history
…ample

ba5e121e7fc4bdae642179fe79e3c76de9a41619 Performance: Major increase in filtering speed

Rather than using `splice` to remove items from the display array for
values which do not match the filtering terms, we now build an array of
the matching items and merge that back to the original array which is
massively faster.

On my computer a search over 6 columns and 10k rows goes from 1.1S to
7mS!

https://datatables.net/forums/discussion/78837

Sync to source repo @ba5e121e7fc4bdae642179fe79e3c76de9a41619
  • Loading branch information
dtbuild committed Apr 26, 2024
1 parent 89f3869 commit bbc78d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion datatables.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
],
"src-repo": "http://github.com/DataTables/DataTablesSrc",
"last-tag": "2.0.5",
"last-sync": "7b8b027e8cf7b49ab5f1df6f0d9d7815d96bf3a8"
"last-sync": "ba5e121e7fc4bdae642179fe79e3c76de9a41619"
}
15 changes: 10 additions & 5 deletions js/dataTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -4422,6 +4422,7 @@
}

var i = 0;
var matched = [];

// Search term can be a function, regex or string - if a string we apply our
// smart filtering regex (assuming the options require that)
Expand All @@ -4433,18 +4434,22 @@
: _fnFilterCreateSearch( input, options );

// Then for each row, does the test pass. If not, lop the row from the array
while (i < searchRows.length) {
for (i=0 ; i<searchRows.length ; i++) {
var row = settings.aoData[ searchRows[i] ];
var data = column === undefined
? row._sFilterRow
: row._aFilterData[ column ];

if ( (searchFunc && ! searchFunc(data, row._aData, searchRows[i], column)) || (rpSearch && ! rpSearch.test(data)) ) {
searchRows.splice(i, 1);
i--;
if ( (searchFunc && searchFunc(data, row._aData, searchRows[i], column)) || (rpSearch && rpSearch.test(data)) ) {
matched.push(searchRows[i]);
}
}

i++;
// Mutate the searchRows array
searchRows.length = matched.length;

for (i=0 ; i<matched.length ; i++) {
searchRows[i] = matched[i];
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/dataTables.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dataTables.min.mjs

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions js/dataTables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4369,6 +4369,7 @@ function _fnFilter( searchRows, settings, input, options, column )
}

var i = 0;
var matched = [];

// Search term can be a function, regex or string - if a string we apply our
// smart filtering regex (assuming the options require that)
Expand All @@ -4380,18 +4381,22 @@ function _fnFilter( searchRows, settings, input, options, column )
: _fnFilterCreateSearch( input, options );

// Then for each row, does the test pass. If not, lop the row from the array
while (i < searchRows.length) {
for (i=0 ; i<searchRows.length ; i++) {
var row = settings.aoData[ searchRows[i] ];
var data = column === undefined
? row._sFilterRow
: row._aFilterData[ column ];

if ( (searchFunc && ! searchFunc(data, row._aData, searchRows[i], column)) || (rpSearch && ! rpSearch.test(data)) ) {
searchRows.splice(i, 1);
i--;
if ( (searchFunc && searchFunc(data, row._aData, searchRows[i], column)) || (rpSearch && rpSearch.test(data)) ) {
matched.push(searchRows[i]);
}
}

i++;
// Mutate the searchRows array
searchRows.length = matched.length;

for (i=0 ; i<matched.length ; i++) {
searchRows[i] = matched[i];
}
}

Expand Down

0 comments on commit bbc78d7

Please sign in to comment.