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

Đang mở Phù hợp với người mới
#201 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, node.js
Lĩnh vực
performance

Hướng nghiên cứu

Công việc nằm ở parseAttribute và hàm hỗ trợ isJSON, hàm này gọi JSON.parse trong try/catch cho mỗi giá trị, cùng với isNumber, hàm này tạo lại RegExp mỗi lần được gọi. Hãy bắt đầu bằng việc đọc ba hàm này và các test hiện có bao phủ parseAttribute. Hoàn thành khi các giá trị không phải JSON trả về mà không ném ngoại lệ, các giá trị JSON được phân tích đúng một lần, và các test hiện có vẫn chạy thành công mà không thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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)
Ngôn ngữ chính
JavaScript
Star
30
Fork
5
Merge trung bình
1 giờ 57 phút
Pull request đã merge (30 ngày)
10

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của webtides/element-js

Tất cả issue của webtides/element-js

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.