Is it possible to find or add an element in one step in a Javascript Map?
I would like to do the following in one step (to avoid looking twice for the right place of the key):
// get the value if the key exists, set a default value otherwise
let aValue = aMap.get(aKey)
if(aValue == null) {
aMap.set(aKey, aDefaultValue)
}
Instead I would like to search for the key only once.
In c++, one can use std::map::insert() or std::map::lower_bound()
In javascript the code could look like this:
let iterator = aMap.getPosition(aKey)
let aValue = aMap.getValue(iterator)
if(aValue == null)
{
aMap.setWithHint(aKey, aValue, iterator)
}
or
let aValue = aMap.getOrSet(aKey, aDefaultValue)
I suppose that it is not possible, but I want to make sure I am correct. Also I am interested in knowing why it is not possible while it is an important feature.
Is it possible to find or add an element in one step in a Javascript Map?
I would like to do the following in one step (to avoid looking twice for the right place of the key):
// get the value if the key exists, set a default value otherwise
let aValue = aMap.get(aKey)
if(aValue == null) {
aMap.set(aKey, aDefaultValue)
}
Instead I would like to search for the key only once.
In c++, one can use std::map::insert() or std::map::lower_bound()
In javascript the code could look like this:
let iterator = aMap.getPosition(aKey)
let aValue = aMap.getValue(iterator)
if(aValue == null)
{
aMap.setWithHint(aKey, aValue, iterator)
}
or
let aValue = aMap.getOrSet(aKey, aDefaultValue)
I suppose that it is not possible, but I want to make sure I am correct. Also I am interested in knowing why it is not possible while it is an important feature.
Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked May 10, 2016 at 9:47 arthur.swarthur.sw 11.7k9 gold badges51 silver badges110 bronze badges 3-
Do I understand you correctly that you are concerned about
aKey
having to be looked up twice in your code example? – nils Commented May 10, 2016 at 10:19 - Yes, this is the problem. – arthur.sw Commented May 10, 2016 at 12:36
- 1 In general, you don't have to worry about these kind of micro-optimizations in JavaScript. Modern JavaScript engines do a pretty good job at handling those. Also, as far as I know, there is no way to prevent this using maps. – nils Commented May 10, 2016 at 12:38
2 Answers
Reset to default 5The lookup has to happen anyway, it doesn't matter much if you avoid it, at least until the engines are optimized much more.
But Map.has
is a nicer solution and should be a bit faster than Map.get()
. For example:
myMap.has(myKey) ? true : myMap.set(myKey, myValue)
Performance should be irrelevant on this level unless you're google-scale. But if it's a serious bottleneck, an Array should still be faster than Map/Set for the forseeable future.
I personally ended up changing my Map to a simple Object. That allows to write a reduce (that groups entries into a Map of Sets) like this:
.reduce((a, [k, v]) => (a[k] = a[k] || new Set()).add(v) ? a : a, {})
With Map it should have bee
.reduce((a, [k, v]) => (a.has(k) ? a : a.set(k, new Set())).get(k).add(v) ? a : a, new Map())
That feels little too cumbersome for this purpose.
I agree that something like this would be ideal if ever supported:
.reduce((a, [k, v]) => a.getOrSet(k, new Set()).add(v) ? a : a, new Map())
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744085152a4556032.html
评论列表(0条)