How to optimize this code? The boss said that it should be converted into ES6 map data structure. My conversion may be wrong and it seems to be slower.
This is the optimization I did, it seems to be slower, please give me some advice
Using filter() can indeed be done in one sentence, but it is not very efficient. In fact, you can use find (refer to MDN)
function getServiceTypeName(code) {
return serviceTypeList.find(val => val.name === code);
}
Unfortunately, IE does not support find(), so there is a Polyfill near the end of the MDN documentation.
If you use map to implement it, you don’t need to use ES6 Map, because the native object supports string type keys, but no matter how it is implemented, the conversion of this map should be done outside getServiceTypeName. Because the conversion process is more time-consuming than what you wrote for ... of.
function toMap(list) {
return list.reduce((map, item) => {
map.set(item.name, item);
return map;
}, new Map());
}
serviceTypeMap = toMap(serviceTypeList);
function getServiceTypeName(code) {
return serviceTypeMap.get(code);
}
First convert it into a map structure with key-value pairs name:Id. Then you can directly use name to get the corresponding id. You didn't understand the meaning of the method he told you at all.
First convert the type array into a map structure, and then get it through map.get(code). No need to traverse.
What solution to converting to map needs to consider the cost of the conversion itself
The map implementation that comes with some languages uses arrays when the collection is small, eliminating the need for hashcode operations and improving efficiency
Using
filter()
can indeed be done in one sentence, but it is not very efficient. In fact, you can usefind
(refer to MDN)Unfortunately, IE does not support
find()
, so there is a Polyfill near the end of the MDN documentation.If you use map to implement it, you don’t need to use ES6 Map, because the native object supports string type keys, but no matter how it is implemented, the conversion of this map should be done outside
getServiceTypeName
. Because the conversion process is more time-consuming than what you wrotefor ... of
.objToStrMap only needs to be initialized once. You are initializing it every time in the loop, which will be slower.
Additional instructions
...
First convert it into a map structure with key-value pairs name:Id. Then you can directly use name to get the corresponding id. You didn't understand the meaning of the method he told you at all.
First convert the type array into a map structure, and then get it through map.get(code). No need to traverse.
In
function, you can write like this
let result = serviceTypeList.map((val)=> val.typeId === code);
retVal = result.name;
Just half a line of code
The operation of filtering in a loop is not slow.
What solution to converting to map needs to consider the cost of the conversion itself
The map implementation that comes with some languages uses arrays when the collection is small, eliminating the need for hashcode operations and improving efficiency