I will use this document to explain everything i tried in 0.12 and ended up not implementing.
At first i tried to change the current JSONDocument and HASHDocument to inherit all of the constructor building and common methods, ex:
abstract class Document {
constructor(schema, ...) {
this.#schema = schema
...
}
#populate() { ... }
abstract toString(): string
public get $id() {...}
// etc...
}
class JSONDocument extends Document { ... }This lead to huge performance issues, this simple change made the Time per Request jump from 0.2ms to almost 0.4ms which lead to more than 30ms increase in batch requests for 1000 documents.
The new method reduces the amount of loops needed from 2 to 1, however doing this increases the model creation time by 0.1ms but has a big plus, it being that it reduces the memory footprint since before the schema was also saved on the map and passed around to the Search class every time. Now the only information passed around are 2 strings per field, its type and its path.
Due to the complex logic the classes do when initialized/constructed converting them into plain objects reduces its performance. The penalty is not as huge as attempt 1 but exists
Removing hidden behavior
Now by default index is set to false, this allows the user to fully control what to save and also improves the speed of json documents and also does not hit the database as hard.
This is quite the challenge, specially when it comes to hashes.
Right now the only parsers i will touch are the JSON ones, being honest im no with much patience to wrap my head around my old HASH code that is working even tho it skips some steps, i will leave hashes for a future attempt.
Tuples will only be transformed into key-value pairs if indexed, this will save tons of time and greatly increase performance. Other than that it just brings a much more readable and easier to maintain structure.
The changes went greatly, now JSON should not have any more edge cases and performance should actually be better than before, tho i will only run benchmarks when i finish attempt 5
In 0.12 i decided to take a new approach to hashes, it being that it will behave exactly like the JSON ones and will only attempt to parse fields that are defined in the Schema, this will be the steps for the parser:
- Has Schema?
- Convert to string following the schema structure
- Objects & Tuples get converted to key-value pairs
- Convert to string following the schema structure
- Call
.toStringorJSON.stringify
This helps speed up parsing hashes and allows the parsing to be way more simple since everything it parses has a known structure.
With that said, there is still some improvements that could be done to merging objects together but im not sure how i will handle that yet.