4 ms·
So I guess we should "transpile" static objects into strings that we call with JSON.parse? Not sure if I should end this comment with a /s or not.
by michaelmcmillan 7y ago
So I guess we should "transpile" static objects into strings that we call with JSON.parse?
Not sure if I should end this comment with a /s or not.
- alxhill 7y agoThat's...what the article proposes, so yes?
- deleted 7y ago[deleted]
- eloff 7y agoSince we compile JavaScript to an unreadable mess anyway... Why not?
- fenwick67 7y agoSometimes you can have massive static objects. Like a list of emojis and their codepoints.[1] It could be useful in cases like that. You'd have to experiment, though. [1] https://raw.githubusercontent.com/joypixels/emoji-toolkit/master/emoji.json https://raw.githubusercontent.com/joypixels/emoji-toolkit/ma...
- alufers 7y agoThe key part here is > As long as the JSON string is only evaluated once the JSON.parse approach is much faster. So doing this would only work on top-level declarations, because the javascript runtime will cache the parsed JSON structure for subsequent executions (eg. creating an object in a loop).
- nostrademons 7y agoWebPack, properly configured, will let you import plain old .json files through the normal es6 import mechanism. It doesn't use this technique though: the JSON file is treated as normal JS source (preprocessed to remove unnecessary quotes etc.) and wrapped with an export.