Search optimisation
These changes make the code more efficient, since going trough an array with `forEach` is faster than iterating with a foor loop on every input change. Using `indexOf()` instead of `includes()` is faster as well.
This commit is contained in:
parent
46d0669be8
commit
aa289de836
@ -1,16 +1,11 @@
|
|||||||
function filter_name(str)
|
function filter_name(str)
|
||||||
{
|
{
|
||||||
if (str.length == 0) {
|
if (str.length == 0) {
|
||||||
for (let i = 0; i < articles.length; i++) {
|
articles.forEach(article => article.style.display = "block");
|
||||||
articles[i].style.display = "block";
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
str = str.toLowerCase();
|
articles.forEach(article => article.style.display = article.dataset.title.indexOf(str.toLowerCase()) === -1 ? "none" : "block");
|
||||||
for (let i = 0; i < articles.length; i++) {
|
|
||||||
articles[i].style.display = !articles[i].dataset.title.includes(str) ? "none" : "block";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let articles = document.getElementsByTagName("article");
|
|
||||||
let search = document.getElementById("search");
|
let articles = Array.from(document.getElementsByTagName("article"));
|
||||||
search.style.display = "inline-block";
|
document.getElementById("search").style.display = "inline-block";
|
||||||
|
Loading…
Reference in New Issue
Block a user