- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Solving the Duplication Crisis
You have inherited a Node.js customer-data pipeline that runs fine on a few hundred records but freezes the server on the production-volume 100,000-row dataset. Every slow function shares the same shape — a `filter` callback that calls `Array.includes` or `Array.indexOf` on a second array, turning each pass into an O(n²) scan. In this lab you'll refactor the offending code to use `Set` and `Map`, so each membership check becomes O(1) on average and the same machine can serve production traffic without freezing.
Lab Info
Table of Contents
-
Challenge
Step 1: Getting started
You have inherited a Node.js customer-data pipeline that runs fine on a few hundred records but freezes the server on the production-volume 100,000-row dataset.
Every slow function shares the same shape: a
filtercallback that callsArray.includesorArray.indexOfon a second array, turning each pass into anO(n²)scan.In this lab you'll refactor the offending code to use
SetandMap, so each membership check becomesO(1)on average and the same machine can serve production traffic without freezing. ### Check the baseline performanceBefore changing any code, run the test suite once so you can see the baseline behavior.
In the terminal, make sure you’re in the
applicationdirectory, then run:./runTest.shThe small-fixture correctness checks pass, but every production-volume check fails its performance budget.
Over the next three steps you'll make each production-volume check pass by reaching for
SetandMapin the six source files undersrc/. > Stuck on a task? Peek at the matchingsolution/stepN/folder for the step you're on to see a working implementation. Try the refactor yourself first; the solution is there as a safety net. info> This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us. -
Challenge
Step 2: Replace membership checks with a Set
The blocklist filter is the most direct example of the performance bug.
Array.includeswalks the blocklist from the start every time it is called, so filtering 100,000 customers against a 10,000-id blocklist can cost up to one billion comparisons.Building a
Setonce turns every subsequent lookup intoO(1)average time, while the rest of the function shape stays the same. -
Challenge
Step 3: Deduplicate primitives with a Set
When you need to deduplicate primitive values, such as strings, numbers, and booleans, a
Setis the right tool.Built-in equality means you don’t need to write a custom key function, and a single pass is enough: check whether each value has been seen, and if it hasn’t, add it.
-
Challenge
Step 4: Deduplicate objects with a Map and intersect with a Set
When the items you want to deduplicate are objects, a plain
Setwon't help because two objects with the same email are still distinct references. The right tool is aMapkeyed by whatever derived value defines uniqueness for your domain.When you need to compute the intersection of two collections, building one of them into a
Setonce turns the whole operation intoO(n + m). -
Challenge
Lab complete
Confirm the full suite is green
Before finishing the lab, run the full test suite to confirm that every refactor works at both small-fixture and production volume. Nicely done. You took a customer-data pipeline that froze on 100,000 records and made it complete the same workload inside a tight performance budget without scaling up the hardware.
You practiced three reusable patterns:
- Build a
Setonce before a hot membership check. - Use a
Setto deduplicate primitives in one pass. - Use a
Mapkeyed by a derived value to deduplicate objects.
Next, try spotting the same shape in your own code. Any loop or
filtercallback that calls.includes(...),.indexOf(...), or.some(...)against another collection may be a candidate for the same refactor. - Build a
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.