π Prefer .getOrInsertComputed() when the default value has side effects.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§ This rule is automatically fixable by the --fix CLI option.
Map#getOrInsert() and WeakMap#getOrInsert() always evaluate the default value, even when the key already exists. Use Map#getOrInsertComputed() or WeakMap#getOrInsertComputed() when creating the default value has side effects.
// β
map.getOrInsert(key, call());
// β
map.getOrInsertComputed(key, () => call());// β
map.getOrInsert(key, call(key));
// β
map.getOrInsertComputed(key, call);