4 ms·
A year ago I needed a min-heap to build a priority queue at work. So first I grabbed 'heap' from npm (272k weekly downloads) and set it to work. But a few days
by idreyn 6y ago
A year ago I needed a min-heap to build a priority queue at work.
So first I grabbed 'heap' from npm (272k weekly downloads) and set it to work. But a few days later I realized my code was executing slower than expected because it sometimes needed to clone the data structure, and the clone instantiation would break the heap invariant in the array internals. It turned out there's been an issue open about this since early 2017.
Then I went for the 'collections' package (35k weekly downloads) and brought in its heap implementation. That worked like a charm for about six months until a bug came in that made it seem like a completely different package was breaking. After almost a whole day of debugging, it turns out that 'collections' silently shims the global Array.from function (scream emoji) without mimicking its behavior when dealing with non-Array iterables presented by the other package.
So finally I wrote my own heap -- well, I cribbed from Eloquent JavaScript [0] but I did have to briefly remember a little bit about how they're supposed to work. So while I don't totally buy the "Never..." rule in the post title, thinking more carefully about writing versus importing a dependency would have saved me a great deal of headache in this case.
[0] https://eloquentjavascript.net/1st_edition/appendix2.html https://eloquentjavascript.net/1st_edition/appendix2.html
- 5Qn8mNbc2FNCiVV 6y agoThis would be a perfect spot to solve those issues and make the packages better... Or y'know, publish your implementation and have it used by people with the same issue
- newen 6y agoWow. Could not be more glad I’m not in the javascript world.