تكرار السؤال: …
الإجابة المختصرة
لأنها عملية متزامنة وحاجبة تفحص الكائن وتحدد أنواعه وقت التشغيل؛ وبدائلها مثل fast-json-stringify تجمع دالة تسلسل مسبقة بناءً على Schema لسرعة مضاعفة.
الشرح
في الخوادم التي ترسل استجابات JSON ضخمة بآلاف الكائنات في الثانية، يستهلك JSON.stringify وقتاً طويلاً من الخيط الرئيسي متسبباً في تأخير الـ Event Loop. استخدام حلول مبنية على JSON Schema يتيح توليد نصوص JSON مباشرة دون الحاجة لاجتياز الكائن وفحص خصائصه ديناميكياً.
مثال بالكود
import fastJson from "fast-json-stringify";
// The serializer is compiled once from a schema — no per-call type sniffing
const stringify = fastJson({
type: "object",
properties: {
id: { type: "integer" },
name: { type: "string" },
tags: { type: "array", items: { type: "string" } },
},
});
const json = stringify({ id: 1, name: "Sara", tags: ["admin"] });
// JSON.stringify is synchronous: serializing a huge object blocks the event loop for every other request.أخطاء شائعة
- تسلسل كائنات ضخمة جداً متزامناً داخل الخيط الرئيسي بدلاً من تدفقها عبر streaming JSON parsers.