1

I have found a way to copy the entries from one Map to another Map where the target Map has multiple reference variables but I suspect it is not optimal. Is there a shorter/more efficient way?

const mapFrom = new Map([[1, 'from']]); const mapTo = new Map([[1, 'to']]); const refMapTo = mapTo; mapTo.clear(); for (const [key, value] of mapFrom) mapTo.set(key, value); if (mapTo.get(1) === mapFrom.get(1) && mapTo === refMapTo) { console.log('this code works but can I avoid the `for...of` iteration?'); } 
3
  • 1
    Why you want to avoid for-of? Commented Feb 1 at 9:15
  • @IvanYonkov because I suspect mapFrom will grow very large and browser performance could be degraded Commented Feb 1 at 9:18
  • 3
    There's no way to copy (at later stage) without looping through the values. Either mapTo must be a reference to mapFrom from the beginning or you need to have this loop one way or another Commented Feb 1 at 9:31

1 Answer 1

1

I don't think there's anything particularly wrong with using for...of. If you really want to avoid it, you could try using forEach, but opinions on this are divided.

Here are a few related topics from earlier.

I don't know the goal of the task. If the goal is simply copying, using the constructor or forEach seems like a good solution. Among the mentioned posts, there are a few that go quite deep into the topic, and I can only recommend reading all of them.

// Constants for map size and test iterations const MAP_SIZE = 100000; const TEST_RUNS = 100; // Create a map with MAP_SIZE entries const mapFrom = new Map(); for (let i = 0; i < MAP_SIZE; i++) { mapFrom.set(i, `value_${i}`); } // Function to run tests TEST_RUNS times and calculate average, min, and max function test(testName, testFunction) { const times = []; // Run the test TEST_RUNS times for (let i = 0; i < TEST_RUNS; i++) { const start = performance.now(); testFunction(); const end = performance.now(); times.push(end - start); } // Calculate average, min, and max const avg = times.reduce((a, b) => a + b, 0) / times.length; const min = Math.min(...times); const max = Math.max(...times); // Log the results console.log(`${testName}:`); console.log(` Average Time: ${avg.toFixed(2)} ms`); console.log(` Min Time: ${min.toFixed(2)} ms`); console.log(` Max Time: ${max.toFixed(2)} ms`); } // 1. Using Map constructor to copy test('Map Constructor', () => { const mapToConstructor = new Map(mapFrom); }); // 2. Using for...of loop to copy test('for...of loop', () => { const mapToForOf = new Map(); for (const [key, value] of mapFrom) { mapToForOf.set(key, value); } }); // 3. Using forEach to copy test('forEach', () => { const mapToForEach = new Map(); mapFrom.forEach((value, key) => { mapToForEach.set(key, value); }); });

Sign up to request clarification or add additional context in comments.

1 Comment

Is iteration obligatory for this requirement?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.