From the course: Creating Angular Libraries

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution

Solution

(upbeat music) - [Instructor] Okay, here's my solution for the challenge. Keep in mind that my solution and your solution may be different, and that's okay. From the root of the workspace, I'll go to projects NGX gem finder, source, lib, utils, and click on calculate average dot TS to open the file. First, I created a constant called cache and set its value to a new weak map. This is where I keep track of calculated averages. I used a weak map here because they accept any non primitive value as a key, which means I can use the input itself as the key for the cache. No need to string apply the data. I get the added benefit of garbage collection with weak maps, but there's some nuance to exactly when keys and values get garbage collected. So I need to be careful and watch for memory leaks. In a real world project, I might experiment with different caching strategies to see what works best for this particular use case. I then export a constant called calculate average and set its value…

Contents