From d6f2d34bf4b9e214c8dff273ba05302766ff9a10 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sat, 18 Jun 2022 22:20:49 +0530 Subject: [PATCH] perf: send boot string instead of JSON literal changes: - compact boot info in /app HTML /app size went from 451kb to 393kb - ~13% less Verified that regex applied on this JSON aren't affecting perf, infact found them to be faster with compact JSON. - Send json string instead of placing JSON literal in code using Jinja. JS takes more time to pass object literal than parsing a plain JSON string. Overall content transfer size remains roughly same (albeit slightly lower) since double escaping ends up adding extra `\` around quotes. Co-authored-by: Suraj Shetty --- frappe/www/app.html | 2 +- frappe/www/app.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/frappe/www/app.html b/frappe/www/app.html index f4de074d32..9189570674 100644 --- a/frappe/www/app.html +++ b/frappe/www/app.html @@ -42,7 +42,7 @@ if (!window.frappe) window.frappe = {}; - frappe.boot = {{ boot }}; + frappe.boot = JSON.parse({{ boot }}); frappe._messages = frappe.boot["__messages"]; frappe.csrf_token = "{{ csrf_token }}"; diff --git a/frappe/www/app.py b/frappe/www/app.py index 9a8c80d6b1..0447d00f89 100644 --- a/frappe/www/app.py +++ b/frappe/www/app.py @@ -2,6 +2,7 @@ # License: MIT. See LICENSE no_cache = 1 +import json import os import re @@ -32,13 +33,14 @@ def get_context(context): frappe.db.commit() - boot_json = frappe.as_json(boot) + boot_json = frappe.as_json(boot, indent=None, separators=(",", ":")) # remove script tags from boot boot_json = SCRIPT_TAG_PATTERN.sub("", boot_json) # TODO: Find better fix boot_json = CLOSING_SCRIPT_TAG_PATTERN.sub("", boot_json) + boot_json = json.dumps(boot_json) context.update( {