parseAttribute: every plain-text attribute throws inside isJSON, which is slow when rendering on the server

Aperta Adatta ai principianti
#201 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js
Ambito
performance

Direzione di ricerca

Il lavoro riguarda parseAttribute e la sua funzione di supporto isJSON, che chiama JSON.parse dentro try/catch per ogni valore, più isNumber, che ricostruisce il proprio RegExp a ogni chiamata. Inizia leggendo queste tre funzioni e i test esistenti che coprono parseAttribute. È completato quando i valori non JSON vengono restituiti senza lanciare eccezioni, i valori JSON vengono analizzati una sola volta e i test esistenti passano invariati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Goal

Reading an attribute that is plain text (a class, a label, an address) costs no thrown exception, on the server as in the browser.

Context

parseAttribute asks isJSON(value) first, and isJSON is try { return JSON.parse(value) && !!value } catch { return false }. Most attribute values are plain text, so most calls throw: the engine builds a SyntaxError with its stack each time, and a value that is JSON is parsed twice, once to test and once to read. isNumber builds its RegExp anew on every call as well.

In the browser this runs once per element at upgrade and nobody notices. With element-js-ssr-renderer it runs for every attribute of every instance on every request, deep inside the recursion of the render, where a stack is long. In a CPU profile of a form page with a few dozen elements parseAttribute was the largest single function, about a seventh of the whole request. On a runtime that limits CPU per request (Cloudflare Workers) that decides whether a page fits.

Acceptance criteria

  • A value that cannot be JSON is returned without an exception being thrown
  • A value that is JSON is parsed once
  • parseAttribute returns for every value what it returns today; the existing tests pass unchanged

Proposal (non-binding)

Look at the value before parsing it: true, false and null by comparison; JSON.parse only where the first character can open JSON ({, [, ", -, a digit), with its result kept instead of parsed again; the number pattern as a constant.

Out of scope

  • What a boolean attribute without a value reads as (#199)
Lingua principale
JavaScript
Stelle
30
Fork
5
Merge medio
1h 57m
PR unite (30g)
10

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di webtides/element-js

Tutte le issue di webtides/element-js

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.