Improve JS performance by storing length before comparing to it in loops

This commit is contained in:
Guillaume Gomez 2020-12-30 16:36:08 +01:00
parent 887398ff68
commit 7cd8128da3
2 changed files with 50 additions and 56 deletions

View File

@ -648,8 +648,7 @@ function defocusSearchBar() {
*/ */
function execQuery(query, searchWords, filterCrates) { function execQuery(query, searchWords, filterCrates) {
function itemTypeFromName(typename) { function itemTypeFromName(typename) {
var length = itemTypes.length; for (var i = 0, len = itemTypes.length; i < len; ++i) {
for (var i = 0; i < length; ++i) {
if (itemTypes[i] === typename) { if (itemTypes[i] === typename) {
return i; return i;
} }
@ -667,8 +666,7 @@ function defocusSearchBar() {
function transformResults(results, isType) { function transformResults(results, isType) {
var out = []; var out = [];
var length = results.length; for (var i = 0, len = results.length; i < len; ++i) {
for (var i = 0; i < length; ++i) {
if (results[i].id > -1) { if (results[i].id > -1) {
var obj = searchIndex[results[i].id]; var obj = searchIndex[results[i].id];
obj.lev = results[i].lev; obj.lev = results[i].lev;
@ -697,11 +695,11 @@ function defocusSearchBar() {
} }
} }
results = ar; results = ar;
var i; var i, len, result;
var nresults = results.length; for (i = 0, len = results.length; i < len; ++i) {
for (i = 0; i < nresults; ++i) { result = results[i];
results[i].word = searchWords[results[i].id]; result.word = searchWords[result.id];
results[i].item = searchIndex[results[i].id] || {}; result.item = searchIndex[result.id] || {};
} }
// if there are no results then return to default and fail // if there are no results then return to default and fail
if (results.length === 0) { if (results.length === 0) {
@ -775,8 +773,7 @@ function defocusSearchBar() {
return 0; return 0;
}); });
var length = results.length; for (i = 0, len = results.length; i < len; ++i) {
for (i = 0; i < length; ++i) {
var result = results[i]; var result = results[i];
// this validation does not make sense when searching by types // this validation does not make sense when searching by types
@ -833,11 +830,10 @@ function defocusSearchBar() {
var vlength = val.generics.length; var vlength = val.generics.length;
for (var y = 0; y < vlength; ++y) { for (var y = 0; y < vlength; ++y) {
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1}; var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
var elength = elems.length;
var firstGeneric = getObjectFromId(val.generics[y]).name; var firstGeneric = getObjectFromId(val.generics[y]).name;
for (var x = 0; x < elength; ++x) { for (var x = 0, elength = elems.length; x < elength; ++x) {
var tmp_lev = levenshtein(getObjectFromId(elems[x]).name, var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
firstGeneric); firstGeneric);
if (tmp_lev < lev.lev) { if (tmp_lev < lev.lev) {
lev.lev = tmp_lev; lev.lev = tmp_lev;
lev.pos = x; lev.pos = x;
@ -861,7 +857,7 @@ function defocusSearchBar() {
// Check for type name and type generics (if any). // Check for type name and type generics (if any).
function checkType(obj, val, literalSearch) { function checkType(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1; var lev_distance = MAX_LEV_DISTANCE + 1;
var x; var len, x, y, e_len, firstGeneric;
if (obj[NAME] === val.name) { if (obj[NAME] === val.name) {
if (literalSearch === true) { if (literalSearch === true) {
if (val.generics && val.generics.length !== 0) { if (val.generics && val.generics.length !== 0) {
@ -870,10 +866,12 @@ function defocusSearchBar() {
var elems = obj[GENERICS_DATA].slice(0); var elems = obj[GENERICS_DATA].slice(0);
var allFound = true; var allFound = true;
for (var y = 0; allFound === true && y < val.generics.length; ++y) { len = val.generics.length;
for (y = 0; allFound === true && y < len; ++y) {
allFound = false; allFound = false;
var firstGeneric = getObjectFromId(val.generics[y]).name; firstGeneric = getObjectFromId(val.generics[y]).name;
for (x = 0; allFound === false && x < elems.length; ++x) { e_len = elems.length;
for (x = 0; allFound === false && x < e_len; ++x) {
allFound = getObjectFromId(elems[x]).name === firstGeneric; allFound = getObjectFromId(elems[x]).name === firstGeneric;
} }
if (allFound === true) { if (allFound === true) {
@ -903,12 +901,10 @@ function defocusSearchBar() {
// Names didn't match so let's check if one of the generic types could. // Names didn't match so let's check if one of the generic types could.
if (literalSearch === true) { if (literalSearch === true) {
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) { if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
var length = obj[GENERICS_DATA].length; return onEach(obj[GENERICS_DATA],
for (x = 0; x < length; ++x) { function(name) {
if (obj[GENERICS_DATA][x] === val.name) { return name === val.name;
return true; });
}
}
} }
return false; return false;
} }
@ -965,7 +961,7 @@ function defocusSearchBar() {
if (typeof ret[0] === "string") { if (typeof ret[0] === "string") {
ret = [ret]; ret = [ret];
} }
for (var x = 0; x < ret.length; ++x) { for (var x = 0, len = ret.length; x < len; ++x) {
var tmp = ret[x]; var tmp = ret[x];
if (typePassesFilter(typeFilter, tmp[1]) === false) { if (typePassesFilter(typeFilter, tmp[1]) === false) {
continue; continue;
@ -1072,23 +1068,22 @@ function defocusSearchBar() {
// aliases to be before the others in the displayed results. // aliases to be before the others in the displayed results.
var aliases = []; var aliases = [];
var crateAliases = []; var crateAliases = [];
var i;
if (filterCrates !== undefined) { if (filterCrates !== undefined) {
if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) { if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) { var query_aliases = ALIASES[filterCrates][query.search];
aliases.push( var len = query_aliases.length;
createAliasFromItem( for (var i = 0; i < len; ++i) {
searchIndex[ALIASES[filterCrates][query.search][i]])); aliases.push(createAliasFromItem(searchIndex[query_aliases[i]]));
} }
} }
} else { } else {
Object.keys(ALIASES).forEach(function(crate) { Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) { if (ALIASES[crate][query.search]) {
var pushTo = crate === window.currentCrate ? crateAliases : aliases; var pushTo = crate === window.currentCrate ? crateAliases : aliases;
for (i = 0; i < ALIASES[crate][query.search].length; ++i) { var query_aliases = ALIASES[crate][query.search];
pushTo.push( var len = query_aliases.length;
createAliasFromItem( for (var i = 0; i < len; ++i) {
searchIndex[ALIASES[crate][query.search][i]])); pushTo.push(createAliasFromItem(searchIndex[query_aliases[i]]));
} }
} }
}); });
@ -1123,11 +1118,12 @@ function defocusSearchBar() {
// quoted values mean literal search // quoted values mean literal search
var nSearchWords = searchWords.length; var nSearchWords = searchWords.length;
var i; var i, it;
var ty; var ty;
var fullId; var fullId;
var returned; var returned;
var in_args; var in_args;
var len;
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") && if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
val.charAt(val.length - 1) === val.charAt(0)) val.charAt(val.length - 1) === val.charAt(0))
{ {
@ -1175,7 +1171,7 @@ function defocusSearchBar() {
var input = parts[0]; var input = parts[0];
// sort inputs so that order does not matter // sort inputs so that order does not matter
var inputs = input.split(",").map(trimmer).sort(); var inputs = input.split(",").map(trimmer).sort();
for (i = 0; i < inputs.length; ++i) { for (i = 0, len = inputs.length; i < len; ++i) {
inputs[i] = extractGenerics(inputs[i]); inputs[i] = extractGenerics(inputs[i]);
} }
var output = extractGenerics(parts[1]); var output = extractGenerics(parts[1]);
@ -1200,7 +1196,7 @@ function defocusSearchBar() {
is_module = true; is_module = true;
} else { } else {
var allFound = true; var allFound = true;
for (var it = 0; allFound === true && it < inputs.length; it++) { for (it = 0, len = inputs.length; allFound === true && it < len; it++) {
allFound = checkType(type, inputs[it], true); allFound = checkType(type, inputs[it], true);
} }
in_args = allFound; in_args = allFound;
@ -1243,7 +1239,7 @@ function defocusSearchBar() {
var paths = valLower.split("::"); var paths = valLower.split("::");
var j; var j;
for (j = 0; j < paths.length; ++j) { for (j = 0, len = paths.length; j < len; ++j) {
if (paths[j] === "") { if (paths[j] === "") {
paths.splice(j, 1); paths.splice(j, 1);
j -= 1; j -= 1;
@ -1365,7 +1361,7 @@ function defocusSearchBar() {
* @return {[boolean]} [Whether the result is valid or not] * @return {[boolean]} [Whether the result is valid or not]
*/ */
function validateResult(name, path, keys, parent) { function validateResult(name, path, keys, parent) {
for (var i = 0; i < keys.length; ++i) { for (var i = 0, len = keys.length; i < len; ++i) {
// each check is for validation so we negate the conditions and invalidate // each check is for validation so we negate the conditions and invalidate
if (!( if (!(
// check for an exact name match // check for an exact name match
@ -1686,7 +1682,7 @@ function defocusSearchBar() {
function getSmallest(arrays, positions, notDuplicates) { function getSmallest(arrays, positions, notDuplicates) {
var start = null; var start = null;
for (var it = 0; it < positions.length; ++it) { for (var it = 0, len = positions.length; it < len; ++it) {
if (arrays[it].length > positions[it] && if (arrays[it].length > positions[it] &&
(start === null || start > arrays[it][positions[it]].lev) && (start === null || start > arrays[it][positions[it]].lev) &&
!notDuplicates[arrays[it][positions[it]].fullPath]) { !notDuplicates[arrays[it][positions[it]].fullPath]) {
@ -1701,7 +1697,7 @@ function defocusSearchBar() {
var positions = []; var positions = [];
var notDuplicates = {}; var notDuplicates = {};
for (var x = 0; x < arrays.length; ++x) { for (var x = 0, arrays_len = arrays.length; x < arrays_len; ++x) {
positions.push(0); positions.push(0);
} }
while (ret.length < MAX_RESULTS) { while (ret.length < MAX_RESULTS) {
@ -1710,7 +1706,7 @@ function defocusSearchBar() {
if (smallest === null) { if (smallest === null) {
break; break;
} }
for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) { for (x = 0; x < arrays_len && ret.length < MAX_RESULTS; ++x) {
if (arrays[x].length > positions[x] && if (arrays[x].length > positions[x] &&
arrays[x][positions[x]].lev === smallest && arrays[x][positions[x]].lev === smallest &&
!notDuplicates[arrays[x][positions[x]].fullPath]) { !notDuplicates[arrays[x][positions[x]].fullPath]) {
@ -1730,7 +1726,7 @@ function defocusSearchBar() {
"others": [], "others": [],
}; };
for (var i = 0; i < queries.length; ++i) { for (var i = 0, len = queries.length; i < len; ++i) {
query = queries[i].trim(); query = queries[i].trim();
if (query.length !== 0) { if (query.length !== 0) {
var tmp = execQuery(getQuery(query), searchWords, filterCrates); var tmp = execQuery(getQuery(query), searchWords, filterCrates);
@ -1884,7 +1880,7 @@ function defocusSearchBar() {
ALIASES[crate][alias_name] = []; ALIASES[crate][alias_name] = [];
} }
local_aliases = aliases[alias_name]; local_aliases = aliases[alias_name];
for (j = 0; j < local_aliases.length; ++j) { for (j = 0, len = local_aliases.length; j < len; ++j) {
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex); ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
} }
} }
@ -2052,8 +2048,7 @@ function defocusSearchBar() {
div.appendChild(h3); div.appendChild(h3);
var ul = document.createElement("ul"); var ul = document.createElement("ul");
var length = filtered.length; for (var i = 0, len = filtered.length; i < len; ++i) {
for (var i = 0; i < length; ++i) {
var item = filtered[i]; var item = filtered[i];
var name = item[0]; var name = item[0];
var desc = item[1]; // can be null var desc = item[1]; // can be null
@ -2122,21 +2117,18 @@ function defocusSearchBar() {
} }
var libs = Object.getOwnPropertyNames(imp); var libs = Object.getOwnPropertyNames(imp);
var llength = libs.length; for (var i = 0, llength = libs.length; i < llength; ++i) {
for (var i = 0; i < llength; ++i) {
if (libs[i] === currentCrate) { continue; } if (libs[i] === currentCrate) { continue; }
var structs = imp[libs[i]]; var structs = imp[libs[i]];
var slength = structs.length;
struct_loop: struct_loop:
for (var j = 0; j < slength; ++j) { for (var j = 0, slength = structs.length; j < slength; ++j) {
var struct = structs[j]; var struct = structs[j];
var list = struct.synthetic ? synthetic_implementors : implementors; var list = struct.synthetic ? synthetic_implementors : implementors;
if (struct.synthetic) { if (struct.synthetic) {
var stlength = struct.types.length; for (var k = 0, stlength = struct.types.length; k < stlength; k++) {
for (var k = 0; k < stlength; k++) {
if (inlined_types.has(struct.types[k])) { if (inlined_types.has(struct.types[k])) {
continue struct_loop; continue struct_loop;
} }
@ -2853,7 +2845,7 @@ function defocusSearchBar() {
return 0; return 0;
}); });
var savedCrate = getSettingValue("saved-filter-crate"); var savedCrate = getSettingValue("saved-filter-crate");
for (var i = 0; i < crates_text.length; ++i) { for (var i = 0, len = crates_text.length; i < len; ++i) {
var option = document.createElement("option"); var option = document.createElement("option");
option.value = crates_text[i]; option.value = crates_text[i];
option.innerText = crates_text[i]; option.innerText = crates_text[i];

View File

@ -8,7 +8,7 @@ function getCurrentFilePath() {
var parts = window.location.pathname.split("/"); var parts = window.location.pathname.split("/");
var rootPathParts = window.rootPath.split("/"); var rootPathParts = window.rootPath.split("/");
for (var i = 0; i < rootPathParts.length; ++i) { for (var i = 0, len = rootPathParts.length; i < len; ++i) {
if (rootPathParts[i] === "..") { if (rootPathParts[i] === "..") {
parts.pop(); parts.pop();
} }
@ -35,12 +35,14 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
}; };
name.innerText = elem["name"]; name.innerText = elem["name"];
var i, len;
var children = document.createElement("div"); var children = document.createElement("div");
children.className = "children"; children.className = "children";
var folders = document.createElement("div"); var folders = document.createElement("div");
folders.className = "folders"; folders.className = "folders";
if (elem.dirs) { if (elem.dirs) {
for (var i = 0; i < elem.dirs.length; ++i) { for (i = 0, len = elem.dirs.length; i < len; ++i) {
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile, if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
hasFoundFile) === true) { hasFoundFile) === true) {
addClass(name, "expand"); addClass(name, "expand");
@ -53,7 +55,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
var files = document.createElement("div"); var files = document.createElement("div");
files.className = "files"; files.className = "files";
if (elem.files) { if (elem.files) {
for (i = 0; i < elem.files.length; ++i) { for (i = 0, len = elem.files.length; i < len; ++i) {
var file = document.createElement("a"); var file = document.createElement("a");
file.innerText = elem.files[i]; file.innerText = elem.files[i];
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html"; file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";