-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should it reuse values? #5
Comments
I did consider this at the beginning; this would of course reduce amount of data transferred on the wire, but there's also time added on comparing values. Comparing may be O(N^2), so a detailed benchmark is needed. I have not yet got enough time to do that. |
Can't you use a Map to store values as you interpolate them? That should take care of the complexity right? |
I plan to add a version that reuses values side-by-side with the current version. If it turned out faster in most use cases I'll consider changing the default, but both versions will be provided. |
A quick-and-dirty implementation is now on branch dedup. Please check how it works out; I will continue to improve it, especially on how it should interoperate with the non-reusing version. |
I recently went and built this feature in one of my own modules, but decided it was a premature optimisation since I wanted to support export type Value = string | number | boolean | object
export type RawValue = Value | Sql
export class Sql {
text: string
values: Value[]
constructor (
protected rawStrings: ReadonlyArray<string>,
protected rawValues: ReadonlyArray<RawValue>
) {
this.values = []
this.text = this.getText(this.values, new Map())
}
getText (values: Value[], cache: Map<Value, number>): string {
return this.rawStrings.reduce((text, part, index) => {
const child = this.rawValues[index - 1]
if (child instanceof Sql) return `${text}${child.getText(values, cache)}${part}`
if (!cache.has(child)) cache.set(child, values.push(child))
return `${text}$${cache.get(child)}${part}`
})
}
} |
Fixed by #8 |
currently results into
Could this be optimized to return
?
The text was updated successfully, but these errors were encountered: