-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Performance
GitHub PR: https://github.com/mongodb/js-bson/pull/338
I have another PR prepared that trivially speeds up the equals method a ton, but wanted to clean the surrounding code up first.
- Since Node.js v6.9.0 is the minimum version, Buffer.from is always available. Allows removing decodeLookup stuff, poor duck-typing of 'id' (this.id.copy was used as a test for it being a buffer).
- Indicate that 12-byte string is allowed as constructor arg.
- Simplify logic in constructor. Performance improvement from not having to call isValid in as many cases and reordering of branches. Creating an ObjectId from a hex string is now ~19% faster.
- Remove a hidden Buffer constructor usage (aliased as new _Buffer)
- Add a TODO regarding an existing bug in getTimestamp. (I want to convert 12-char strings to Buffers in the ctor to fix this and clean other code up, but that would be a breaking change because it would make the constructor throw instead of toHexString. Throwing in the ctor seems preferable...)
- Simplify createFromHexString. (This is the same as the constructor but requires the input be a hex string.)
- Simplify logic (remove redundant and unnecessary code) in equals method.