对于一个常见优化问题的思考
1. 问题
js
let arr1 = [
{ id: 1234, code: 1, type: "TYPE1" }
];
let arr2 = [
{ code: 1, title: "title" }
];
//其中arr2是arr1的子集,且arr2里的code都会在arr1中出现
// let list = arr1.map(item => {
// let title
// arr2.forEach(i => {
// if(i.code === item.code) title = i.title
// })
// return {
// title: title,
// type: item.type,
// id: item.id
// }
// })
let codeTitleMap = new Map(
arr2.map(({ code, title }) => [code, title])
);
let list = arr1.map((code, ...args) => ({
title: codeTitleMap.get(code),
...args
}));
2. 常用解决方案
3. 优化方案
4. 时间复杂度分析与前后优化性能对比
5. 为什么可以优化?
6. 扩展
LRU算法 + 双向链表 + vue keep-alive的实现