Why aren't interpolation values in tagged template calls saved as a cached array?

#!/JoePea joe at trusktr.io
Sun Jul 26 18:08:10 UTC 2020


The following doesn't work either, and obviously it probably will
never change because it is historical:

```js
argsArrays = []

function html() {
    argsArrays.push(arguments)
}

let n = 0
let n2 = 0

function render() { return html`1st: ${n++}, 2nd: ${n2++}.` }

render()
render()

console.log(argsArrays[0] === argsArrays[1]) // false!
```

So yeah, just wondering why our only option is to use `...args` and
create new arrays each time, thus the performance is not optimized
like it is with the string parts.

#!/JoePea

On Sun, Jul 26, 2020 at 10:49 AM #!/JoePea <joe at trusktr.io> wrote:
>
> What I mean is,
>
> We can currently do this (try it in console):
>
> ```js
> arrays = []
>
> function html(parts) { arrays.push(parts) }
>
> let n = 0
> let n2 = 0
>
> function render() { return html`1st: ${n++}, 2nd: ${n2++}.` }
>
> render()
> render()
>
> console.log(arrays[0] === arrays[1]) // true! <------------------- This!
> ```
>
> But why don't specs allows us to do the following as well? (don't run
> it, it doesn't work as intended)
>
> ```js
> partsArrays = []
> valuesArrays = []
>
> function html(parts, values) {
>     partsArrays.push(parts)
>     valuesArrays.push(values)
> }
>
> let n = 0
> let n2 = 0
>
> function render() { return html`1st: ${n++}, 2nd: ${n2++}.` }
>
> render()
> render()
>
> console.log(partsArrays[0] === partsArrays[1]) // true!
> console.log(valuesArrays[0] === valuesArrays[1]) // This would be
> convenient too! I think?
> ```
>
> Instead, if we want an array of the values, we have to use `arguments`
> or `...args`. For example:
>
> ```js
> // ... same ...
> function html(parts, ...values) {
>     partsArrays.push(parts)
>     valuesArrays.push(values)
> }
> // ... same ...
> console.log(partsArrays[0] === partsArrays[1]) // true!
> console.log(valuesArrays[0] === valuesArrays[1]) // false! New array every time.
> ```
>
> Seems like it would've been great to have the cached values arrays
> too. Why isn't this the case?
>
> #!/JoePea


More information about the es-discuss mailing list