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

Micro-optimize search #482

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
43 changes: 28 additions & 15 deletions dist/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,23 @@ var List = function(id, options, values) {

this.update = function() {
var is = self.items,
il = is.length;
il = is.length,
frag = document.createDocumentFragment();

self.visibleItems = [];
self.matchingItems = [];
self.templater.clear();
for (var i = 0; i < il; i++) {
if (is[i].matching() && ((self.matchingItems.length+1) >= self.i && self.visibleItems.length < self.page)) {
is[i].show();
self.templater.create(is[i]);
frag.appendChild(is[i].elm);
self.visibleItems.push(is[i]);
self.matchingItems.push(is[i]);
} else if (is[i].matching()) {
self.matchingItems.push(is[i]);
is[i].hide();
} else {
is[i].hide();
}
}
self.list.appendChild(frag);
self.trigger('updated');
return self;
};
Expand Down Expand Up @@ -430,11 +430,13 @@ module.exports = function(list) {
};

},{"./item":4}],6:[function(require,module,exports){
var REGEX_CHARACTERS_PATTERN = /[-[\]{}()*+?.,\\^$|#]/g;

module.exports = function(list) {
var item,
text,
columns,
searchString,
searchPattern,
customSearch;

var prepare = {
Expand Down Expand Up @@ -462,10 +464,14 @@ module.exports = function(list) {
columns = (list.searchColumns === undefined) ? prepare.toArray(list.items[0].values()) : list.searchColumns;
}
},
setSearchString: function(s) {
setSearchPattern: function(s) {
s = list.utils.toString(s).toLowerCase();
s = s.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"); // Escape regular expression characters
searchString = s;
if (s === "") {
searchPattern = undefined;
return;
}
s = s.replace(REGEX_CHARACTERS_PATTERN, "\\$&"); // Escape regular expression characters
searchPattern = new RegExp(s, 'i');
},
toArray: function(values) {
var tmpColumn = [];
Expand All @@ -492,8 +498,8 @@ module.exports = function(list) {
},
values: function(values, column) {
if (values.hasOwnProperty(column)) {
text = list.utils.toString(values[column]).toLowerCase();
if ((searchString !== "") && (text.search(searchString) > -1)) {
text = list.utils.toString(values[column]);
if (searchPattern && searchPattern.test(text)) {
return true;
}
}
Expand All @@ -509,16 +515,16 @@ module.exports = function(list) {
list.trigger('searchStart');

prepare.resetList();
prepare.setSearchString(str);
prepare.setSearchPattern(str);
prepare.setOptions(arguments); // str, cols|searchFunction, searchFunction
prepare.setColumns();

if (searchString === "" ) {
if (!searchPattern) {
search.reset();
} else {
list.searched = true;
if (customSearch) {
customSearch(searchString, columns);
customSearch(searchPattern, columns);
} else {
search.list();
}
Expand Down Expand Up @@ -778,7 +784,7 @@ var Templater = function(list) {
return false;
}
if (itemSource === undefined) {
throw new Error("The list need to have at list one item on init otherwise you'll have to add a template.");
throw new Error("The list needs to have at least one item on init otherwise you'll have to add a template.");
}
/* If item source does not exists, use the first item in list as
source for new items */
Expand All @@ -803,6 +809,13 @@ var Templater = function(list) {
}
};
this.clear = function() {
if (typeof document.createRange === 'function') {
// Slightly faster, but not supported in IE<9
var range = document.createRange();
range.selectNodeContents(list.list);
range.deleteContents();
return;
}
/* .innerHTML = ''; fucks up IE */
if (list.list.hasChildNodes()) {
while (list.list.childNodes.length >= 1)
Expand Down
2 changes: 1 addition & 1 deletion dist/list.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/javascripts/list.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,23 @@ var List = function(id, options, values) {

this.update = function() {
var is = self.items,
il = is.length;
il = is.length,
frag = document.createDocumentFragment();

self.visibleItems = [];
self.matchingItems = [];
self.templater.clear();
for (var i = 0; i < il; i++) {
if (is[i].matching() && ((self.matchingItems.length+1) >= self.i && self.visibleItems.length < self.page)) {
is[i].show();
self.templater.create(is[i]);
frag.appendChild(is[i].elm);
self.visibleItems.push(is[i]);
self.matchingItems.push(is[i]);
} else if (is[i].matching()) {
self.matchingItems.push(is[i]);
is[i].hide();
} else {
is[i].hide();
}
}
self.list.appendChild(frag);
self.trigger('updated');
return self;
};
Expand Down
24 changes: 15 additions & 9 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var REGEX_CHARACTERS_PATTERN = /[-[\]{}()*+?.,\\^$|#]/g;

module.exports = function(list) {
var item,
text,
columns,
searchString,
searchPattern,
customSearch;

var prepare = {
Expand Down Expand Up @@ -30,10 +32,14 @@ module.exports = function(list) {
columns = (list.searchColumns === undefined) ? prepare.toArray(list.items[0].values()) : list.searchColumns;
}
},
setSearchString: function(s) {
setSearchPattern: function(s) {
s = list.utils.toString(s).toLowerCase();
s = s.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"); // Escape regular expression characters
searchString = s;
if (s === "") {
searchPattern = undefined;
return;
}
s = s.replace(REGEX_CHARACTERS_PATTERN, "\\$&"); // Escape regular expression characters
searchPattern = new RegExp(s, 'i');
},
toArray: function(values) {
var tmpColumn = [];
Expand All @@ -60,8 +66,8 @@ module.exports = function(list) {
},
values: function(values, column) {
if (values.hasOwnProperty(column)) {
text = list.utils.toString(values[column]).toLowerCase();
if ((searchString !== "") && (text.search(searchString) > -1)) {
text = list.utils.toString(values[column]);
if (searchPattern && searchPattern.test(text)) {
return true;
}
}
Expand All @@ -77,16 +83,16 @@ module.exports = function(list) {
list.trigger('searchStart');

prepare.resetList();
prepare.setSearchString(str);
prepare.setSearchPattern(str);
prepare.setOptions(arguments); // str, cols|searchFunction, searchFunction
prepare.setColumns();

if (searchString === "" ) {
if (!searchPattern) {
search.reset();
} else {
list.searched = true;
if (customSearch) {
customSearch(searchString, columns);
customSearch(searchPattern, columns);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a breaking change, depending on how people use the customSearch option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

searchPattern.source could help.

} else {
search.list();
}
Expand Down
9 changes: 8 additions & 1 deletion src/templater.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var Templater = function(list) {
return false;
}
if (itemSource === undefined) {
throw new Error("The list need to have at list one item on init otherwise you'll have to add a template.");
throw new Error("The list needs to have at least one item on init otherwise you'll have to add a template.");
}
/* If item source does not exists, use the first item in list as
source for new items */
Expand All @@ -157,6 +157,13 @@ var Templater = function(list) {
}
};
this.clear = function() {
if (typeof document.createRange === 'function') {
// Slightly faster, but not supported in IE<9
var range = document.createRange();
range.selectNodeContents(list.list);
range.deleteContents();
return;
}
/* .innerHTML = ''; fucks up IE */
if (list.list.hasChildNodes()) {
while (list.list.childNodes.length >= 1)
Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<head>
<meta charset="utf-8">
<title>List.js Mocha Tests</title>
<link rel="stylesheet" href="../node_modules/grunt-mocha/node_modules/mocha/mocha.css" />
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
<style>
#list,
#parse-list,
Expand All @@ -17,7 +17,7 @@
<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src="../dist/list.js"></script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../node_modules/grunt-mocha/node_modules/mocha/mocha.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>

<script>
mocha.setup('bdd');
Expand Down