Most performant way to copy a Map into an existing MapIs there an “exists” function for jQuery?How do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesWhat is the most efficient way to deep clone an object in JavaScript?What is the best way to iterate over a dictionary?How do I copy to the clipboard in JavaScript?Checking if a key exists in a JavaScript object?Check if a given key already exists in a dictionaryHow to clone or copy a list?Round to at most 2 decimal places (only if necessary)

How do Barton (Hawkeye/Ronin) and Romanov (Black Widow) end up on the Benatar on Morag in 2014?

STM32 cannot reach individual registers and pins as PIC

What to do about my 1-month-old boy peeing through diapers?

Shall I fix cracks on bathtub and how to fix them?

Are spot colors limited and why CMYK mix is not treated same as spot color mix?

Count the number of triangles

Why is there no Disney logo in MCU movies?

Is this password scheme legit?

Journal published a paper, ignoring my objections as a referee

Get contents before a colon

Why does the weaker C–H bond have a higher wavenumber than the C=O bond?

Why is there not a willingness from the world to step in between Pakistan and India?

Why did Lucius make a deal out of Buckbeak hurting Draco but not about Draco being turned into a ferret?

Why is 3/4 a simple meter while 6/8 is a compound meter?

How to say "I only speak one language which is English" in French?

Another "Ask One Question" Question

Is the Amazon rainforest the "world's lungs"?

How to handle inventory and story of a player leaving

Difference between big data and high dimensional data

Pen test results for web application include a file from a forbidden directory that is not even used or referenced

Should I ask for a raise one month before the end of an internship?

Why might one *not* want to use a capo?

Why can't you say don't instead of won't?

What should be done with the carbon when using magic to get oxygen from carbon dioxide?



Most performant way to copy a Map into an existing Map


Is there an “exists” function for jQuery?How do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesWhat is the most efficient way to deep clone an object in JavaScript?What is the best way to iterate over a dictionary?How do I copy to the clipboard in JavaScript?Checking if a key exists in a JavaScript object?Check if a given key already exists in a dictionaryHow to clone or copy a list?Round to at most 2 decimal places (only if necessary)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I want to overwrite Map b in the snippet below with the contents of Map a. I am currently clearing the contents of b and then setting the entries one by one. What is a more efficient way (less garbage, faster, etc.) of writing the copyMap function?






const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);












share|improve this question


























  • Do you want to mutate b, or would you be fine with creating a new Map?

    – Bergi
    Mar 27 at 13:50






  • 1





    I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

    – Bergi
    Mar 27 at 13:52






  • 1





    My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

    – Cristy
    Mar 27 at 14:18






  • 1





    No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

    – Bergi
    Mar 27 at 18:47







  • 1





    I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

    – Cristy
    Mar 27 at 20:53

















4















I want to overwrite Map b in the snippet below with the contents of Map a. I am currently clearing the contents of b and then setting the entries one by one. What is a more efficient way (less garbage, faster, etc.) of writing the copyMap function?






const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);












share|improve this question


























  • Do you want to mutate b, or would you be fine with creating a new Map?

    – Bergi
    Mar 27 at 13:50






  • 1





    I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

    – Bergi
    Mar 27 at 13:52






  • 1





    My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

    – Cristy
    Mar 27 at 14:18






  • 1





    No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

    – Bergi
    Mar 27 at 18:47







  • 1





    I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

    – Cristy
    Mar 27 at 20:53













4












4








4








I want to overwrite Map b in the snippet below with the contents of Map a. I am currently clearing the contents of b and then setting the entries one by one. What is a more efficient way (less garbage, faster, etc.) of writing the copyMap function?






const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);












share|improve this question
















I want to overwrite Map b in the snippet below with the contents of Map a. I am currently clearing the contents of b and then setting the entries one by one. What is a more efficient way (less garbage, faster, etc.) of writing the copyMap function?






const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);








const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);





const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap)
destMap.clear();

// Maybe this loop could be done in a single call somehow?
for(let item of srcMap)
destMap.set(item[0], item[1]);


copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);






javascript dictionary ecmascript-6 clone






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 14:22







Cristy

















asked Mar 27 at 13:24









CristyCristy

14.8k20 gold badges69 silver badges117 bronze badges




14.8k20 gold badges69 silver badges117 bronze badges















  • Do you want to mutate b, or would you be fine with creating a new Map?

    – Bergi
    Mar 27 at 13:50






  • 1





    I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

    – Bergi
    Mar 27 at 13:52






  • 1





    My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

    – Cristy
    Mar 27 at 14:18






  • 1





    No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

    – Bergi
    Mar 27 at 18:47







  • 1





    I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

    – Cristy
    Mar 27 at 20:53

















  • Do you want to mutate b, or would you be fine with creating a new Map?

    – Bergi
    Mar 27 at 13:50






  • 1





    I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

    – Bergi
    Mar 27 at 13:52






  • 1





    My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

    – Cristy
    Mar 27 at 14:18






  • 1





    No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

    – Bergi
    Mar 27 at 18:47







  • 1





    I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

    – Cristy
    Mar 27 at 20:53
















Do you want to mutate b, or would you be fine with creating a new Map?

– Bergi
Mar 27 at 13:50





Do you want to mutate b, or would you be fine with creating a new Map?

– Bergi
Mar 27 at 13:50




1




1





I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

– Bergi
Mar 27 at 13:52





I don't think copyMap could get any simpler than what you did. (Maybe use destructuring for item, but that's just syntax).

– Bergi
Mar 27 at 13:52




1




1





My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

– Cristy
Mar 27 at 14:18





My main goal is to reduce garbage generation, as it is for a high-performance app where GC pauses can ruin user experience and I want to re-use as much of the already instantiated objects as possible. I do think (from previous experiences) that changing an existing object is faster and generates less garbage than creating a new one.

– Cristy
Mar 27 at 14:18




1




1





No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

– Bergi
Mar 27 at 18:47






No, Maps don't have a bulk copy method. The only method that takes an iterable is the constructor. I don't think it does pre-allocate the memory to the expected size, but you could still try to do b = new Map(a).

– Bergi
Mar 27 at 18:47





1




1





I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

– Cristy
Mar 27 at 20:53





I have multiple app instances (tens or hundreds) running at 60FPS on a single thread, every several frames a new a is received (different for each instance) and I have to save it/copy it into b. In my specific case, both a and b have (mostly) the same keys (but different values for those keys), each time the copy function is used, so probably it's better not to clear the map before, but only set/overwrite current keys.

– Cristy
Mar 27 at 20:53












1 Answer
1






active

oldest

votes


















0















you can jsut do



b=new Map(a)


and you need to loose the const declaration for b






share|improve this answer

























  • But this way previous b Map will become garbage, and I want to avoid creating garbage.

    – Cristy
    Mar 28 at 9:08











  • then why not copy it in a new variable ?

    – jonathan Heindl
    Mar 28 at 9:11











  • What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

    – Cristy
    Mar 28 at 9:12












  • See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

    – benvc
    Mar 29 at 2:01










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378312%2fmost-performant-way-to-copy-a-map-into-an-existing-map%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















you can jsut do



b=new Map(a)


and you need to loose the const declaration for b






share|improve this answer

























  • But this way previous b Map will become garbage, and I want to avoid creating garbage.

    – Cristy
    Mar 28 at 9:08











  • then why not copy it in a new variable ?

    – jonathan Heindl
    Mar 28 at 9:11











  • What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

    – Cristy
    Mar 28 at 9:12












  • See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

    – benvc
    Mar 29 at 2:01















0















you can jsut do



b=new Map(a)


and you need to loose the const declaration for b






share|improve this answer

























  • But this way previous b Map will become garbage, and I want to avoid creating garbage.

    – Cristy
    Mar 28 at 9:08











  • then why not copy it in a new variable ?

    – jonathan Heindl
    Mar 28 at 9:11











  • What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

    – Cristy
    Mar 28 at 9:12












  • See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

    – benvc
    Mar 29 at 2:01













0














0










0









you can jsut do



b=new Map(a)


and you need to loose the const declaration for b






share|improve this answer













you can jsut do



b=new Map(a)


and you need to loose the const declaration for b







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 23:16









jonathan Heindljonathan Heindl

6342 silver badges13 bronze badges




6342 silver badges13 bronze badges















  • But this way previous b Map will become garbage, and I want to avoid creating garbage.

    – Cristy
    Mar 28 at 9:08











  • then why not copy it in a new variable ?

    – jonathan Heindl
    Mar 28 at 9:11











  • What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

    – Cristy
    Mar 28 at 9:12












  • See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

    – benvc
    Mar 29 at 2:01

















  • But this way previous b Map will become garbage, and I want to avoid creating garbage.

    – Cristy
    Mar 28 at 9:08











  • then why not copy it in a new variable ?

    – jonathan Heindl
    Mar 28 at 9:11











  • What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

    – Cristy
    Mar 28 at 9:12












  • See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

    – benvc
    Mar 29 at 2:01
















But this way previous b Map will become garbage, and I want to avoid creating garbage.

– Cristy
Mar 28 at 9:08





But this way previous b Map will become garbage, and I want to avoid creating garbage.

– Cristy
Mar 28 at 9:08













then why not copy it in a new variable ?

– jonathan Heindl
Mar 28 at 9:11





then why not copy it in a new variable ?

– jonathan Heindl
Mar 28 at 9:11













What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

– Cristy
Mar 28 at 9:12






What do you mean, I just explained above? I do the copyMap operation hundreds of times a second, if I create a new Map each time it will do a lot of allocations and create a lot of garbage.

– Cristy
Mar 28 at 9:12














See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

– benvc
Mar 29 at 2:01





See Memory Management to better understand the garbage collection issue that OP is trying to solve here.

– benvc
Mar 29 at 2:01








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378312%2fmost-performant-way-to-copy-a-map-into-an-existing-map%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript