1
0

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:
Emil Miler 2022-12-09 13:17:29 +01:00
parent 46d0669be8
commit aa289de836

View File

@ -1,16 +1,11 @@
function filter_name(str)
{
if (str.length == 0) {
for (let i = 0; i < articles.length; i++) {
articles[i].style.display = "block";
}
articles.forEach(article => article.style.display = "block");
} else {
str = str.toLowerCase();
for (let i = 0; i < articles.length; i++) {
articles[i].style.display = !articles[i].dataset.title.includes(str) ? "none" : "block";
articles.forEach(article => article.style.display = article.dataset.title.indexOf(str.toLowerCase()) === -1 ? "none" : "block");
}
}
}
let articles = document.getElementsByTagName("article");
let search = document.getElementById("search");
search.style.display = "inline-block";
let articles = Array.from(document.getElementsByTagName("article"));
document.getElementById("search").style.display = "inline-block";