【发布时间】:2016-02-04 17:22:06
【问题描述】:
使用数组(可能经常被推送和拼接)作为 ES6 Map 中的值是否有意义,还是应该坚持使用普通关联数组?
例如,类似于以下节点服务器代码的代码,它将跟踪哪些用户已经下载了特定文件
// initialisation
var map = new Map();
map.set ("foo.png",[]);
// push a new user id to array associated with hashString
function userHasFile(filename,userId) {
// retrieve array of all userIds that have downloaded the file
var users = map.get(filename);
// delete the map reference since we are going to update it
map.delete(filename);
// push the new userId to the array of users that have downloaded file
users.push(userId);
// update the Map key to the new array
map.set(filename,users);
}
// example usage
userHasFile("foo.png","user1");
userHasFile("foo.png","user2");
我是 ES6 新手,Map API 的简单性对我很有吸引力,所以我想知道这种方法与使用标准关联数组相比是否有任何好处\缺点(例如性能、内存、GC 等) .
【问题讨论】:
标签: javascript arrays ecmascript-6