-
Notifications
You must be signed in to change notification settings - Fork 329
Skyhight173/json: add cache to improve performance #2457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LegoBrainBiker
wants to merge
7
commits into
TurboWarp:master
Choose a base branch
from
LegoBrainBiker:patch-4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb33849
Skyhight173/json: add cache to improve performance
LegoBrainBiker 5093b21
[Automated] Format code
DangoCat 1ec5492
Skyhight173/json: also cache on parse
LegoBrainBiker 26ce230
[Automated] Format code
DangoCat 6959b15
Skyhight173/json: clear cache on an interval
LegoBrainBiker 69c46bc
[Automated] Format code
DangoCat 6dfaca1
Skyhight173/json: put the parameters in the right order
LegoBrainBiker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,25 @@ | |
| */ | ||
|
|
||
| const vm = Scratch.vm; | ||
| const objectCache = {}; | ||
| function parse(string) { | ||
| return ( | ||
| objectCache[string] ?? | ||
| (setTimeout(() => { | ||
| delete objectCache[string]; | ||
| }, 100), | ||
| (objectCache[string] = JSON.parse(string))) | ||
| ); | ||
| } | ||
| function stringify(object) { | ||
| const string = JSON.stringify(object); | ||
| objectCache[string] = object; | ||
| setTimeout(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. find a way to not be making a new timeout for every single string |
||
| delete objectCache[string]; | ||
| }, 100); // store it in the cache for 100 ms | ||
| return string; | ||
| } | ||
|
|
||
| const hasOwn = (obj, property) => | ||
| Object.prototype.hasOwnProperty.call(obj, property); | ||
|
|
||
|
|
@@ -661,7 +680,7 @@ | |
| return false; | ||
| } else { | ||
| try { | ||
| JSON.parse(json); | ||
| parse(json); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
|
|
@@ -680,7 +699,7 @@ | |
| return json; | ||
| } else { | ||
| try { | ||
| return JSON.parse(json) ?? ""; | ||
| return parse(json) ?? ""; | ||
| } catch { | ||
| return json; | ||
| } | ||
|
|
@@ -690,7 +709,7 @@ | |
| json_is({ json, types }) { | ||
| if (!this.json_is_valid({ json: json })) return false; | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| switch (types) { | ||
| case "Object": | ||
| return !Array.isArray(json); | ||
|
|
@@ -706,7 +725,7 @@ | |
|
|
||
| json_length({ json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| return Object.keys(json).length; | ||
| } catch { | ||
| return " "; | ||
|
|
@@ -727,8 +746,7 @@ | |
| json_has_key({ json, key }) { | ||
| try { | ||
| return ( | ||
| this._fixInvalidJSONValues(this.json_valid_return(key)) in | ||
| JSON.parse(json) | ||
| this._fixInvalidJSONValues(this.json_valid_return(key)) in parse(json) | ||
| ); | ||
| } catch { | ||
| return false; | ||
|
|
@@ -737,7 +755,7 @@ | |
|
|
||
| json_has_value({ json, value }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| value = this.json_valid_return(value); | ||
| return json.includes(value); | ||
| } catch { | ||
|
|
@@ -747,8 +765,8 @@ | |
|
|
||
| json_equal({ json1, equal, json2 }) { | ||
| try { | ||
| json1 = JSON.parse(json1); | ||
| json2 = JSON.parse(json2); | ||
| json1 = parse(json1); | ||
| json2 = parse(json2); | ||
|
|
||
| const keys1 = Object.keys(json1); | ||
| const keys2 = Object.keys(json2); | ||
|
|
@@ -765,16 +783,14 @@ | |
|
|
||
| json_get_all({ Stype, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| switch (Stype) { | ||
| case "keys": | ||
| return JSON.stringify(Object.keys(json).map((key) => key ?? "")); | ||
| return stringify(Object.keys(json).map((key) => key ?? "")); | ||
| case "values": | ||
| return JSON.stringify( | ||
| Object.keys(json).map((key) => json[key] ?? "") | ||
| ); | ||
| return stringify(Object.keys(json).map((key) => json[key] ?? "")); | ||
| case "datas": | ||
| return JSON.stringify( | ||
| return stringify( | ||
| Object.keys(json).map((key) => [key, json[key] ?? ""]) | ||
| ); | ||
| default: | ||
|
|
@@ -787,11 +803,11 @@ | |
|
|
||
| json_get({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| if (hasOwn(json, item)) { | ||
| const result = json[item] ?? ""; | ||
| if (typeof result === "object") { | ||
| return JSON.stringify(result); | ||
| return stringify(result); | ||
| } else { | ||
| return result; | ||
| } | ||
|
|
@@ -813,21 +829,21 @@ | |
|
|
||
| json_set({ item, value, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| value = this.json_valid_return(value); | ||
| value = this._fixInvalidJSONValues(value); | ||
| json[item] = value; | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_delete({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| delete json[item]; | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
|
|
@@ -846,7 +862,7 @@ | |
| if (item > 0) { | ||
| item--; | ||
| } | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| let result; | ||
| if (item >= 0) { | ||
| result = json[item]; | ||
|
|
@@ -856,7 +872,7 @@ | |
| } | ||
| result = result ?? ""; | ||
| if (typeof result == "object") { | ||
| return JSON.stringify(result); | ||
| return stringify(result); | ||
| } else { | ||
| return result; | ||
| } | ||
|
|
@@ -867,9 +883,9 @@ | |
|
|
||
| json_array_itemH({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| item = this._fixInvalidJSONValues(this.json_valid_return(item)); | ||
| let result = JSON.stringify(json.indexOf(item) + 1); | ||
| let result = stringify(json.indexOf(item) + 1); | ||
| return result; | ||
| } catch { | ||
| return ""; | ||
|
|
@@ -878,69 +894,69 @@ | |
|
|
||
| json_array_from({ json }) { | ||
| try { | ||
| return JSON.stringify(Array.from(String(json))); | ||
| return stringify(Array.from(String(json))); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_concat({ json, json2 }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json2 = JSON.parse(json2); | ||
| return JSON.stringify(json.concat(json2)); | ||
| json = parse(json); | ||
| json2 = parse(json2); | ||
| return stringify(json.concat(json2)); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_push({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| item = this._fixInvalidJSONValues(this.json_valid_return(item)); | ||
| json.push(item); | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_insert({ item, pos, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| item = this._fixInvalidJSONValues(this.json_valid_return(item)); | ||
| json.splice(pos - 1, 0, item); | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_set({ item, pos, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| json[pos - 1] = this._fixInvalidJSONValues( | ||
| this.json_valid_return(item) | ||
| ); | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_delete({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| json.splice(item - 1, 1); | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_remove_all({ item, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| item = this._fixInvalidJSONValues(this.json_valid_return(item)); | ||
| let i = 0; | ||
| while (i < json.length) { | ||
|
|
@@ -950,52 +966,52 @@ | |
| ++i; | ||
| } | ||
| } | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_fromto({ json, item, item2 }) { | ||
| try { | ||
| return JSON.stringify(JSON.parse(json).slice(item - 1, item2)); | ||
| return stringify(parse(json).slice(item - 1, item2)); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_reverse({ json }) { | ||
| try { | ||
| return JSON.stringify(JSON.parse(json).reverse()); | ||
| return stringify(parse(json).reverse()); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_flat({ json, depth }) { | ||
| try { | ||
| return JSON.stringify(JSON.parse(json).flat(depth)); | ||
| return stringify(parse(json).flat(depth)); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_create({ text, d }) { | ||
| return JSON.stringify(String(text).split(d)); | ||
| return stringify(String(text).split(d)); | ||
| } | ||
|
|
||
| json_array_join({ json, d }) { | ||
| try { | ||
| return JSON.parse(json).join(d); | ||
| return parse(json).join(d); | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| json_array_filter({ key, json }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| return JSON.stringify( | ||
| json = parse(json); | ||
| return stringify( | ||
| json.map((x) => { | ||
| if (hasOwn(x, key)) { | ||
| return x[key]; | ||
|
|
@@ -1010,9 +1026,9 @@ | |
|
|
||
| json_array_setlen({ json, len }) { | ||
| try { | ||
| json = JSON.parse(json); | ||
| json = parse(json); | ||
| json.length = len; | ||
| return JSON.stringify(json); | ||
| return stringify(json); | ||
| } catch { | ||
| return ""; | ||
| } | ||
|
|
@@ -1022,7 +1038,7 @@ | |
| try { | ||
| let listVariable = this.lookupList(list, util); | ||
| if (listVariable) { | ||
| return JSON.stringify(listVariable.value); | ||
| return stringify(listVariable.value); | ||
| } | ||
| } catch (e) { | ||
| // ignore | ||
|
|
@@ -1033,10 +1049,10 @@ | |
| try { | ||
| let listVariable = this.lookupList(list, util); | ||
| if (listVariable) { | ||
| const array = JSON.parse(json); | ||
| const array = parse(json); | ||
| if (Array.isArray(array)) { | ||
| const safeArray = array.map((i) => { | ||
| if (typeof i === "object") return JSON.stringify(i); | ||
| if (typeof i === "object") return stringify(i); | ||
| return i ?? ""; | ||
| }); | ||
| listVariable.value = safeArray; | ||
|
|
@@ -1051,7 +1067,7 @@ | |
| json_array_sort(args) { | ||
| let list; | ||
| try { | ||
| list = JSON.parse(args.list); | ||
| list = parse(args.list); | ||
| } catch { | ||
| return ""; | ||
| } | ||
|
|
@@ -1060,12 +1076,12 @@ | |
| } | ||
| list.sort(Scratch.Cast.compare); | ||
| if (args.order === "descending") list.reverse(); | ||
| return JSON.stringify(list); | ||
| return stringify(list); | ||
| } | ||
| json_array_analysis(args) { | ||
| let list; | ||
| try { | ||
| list = JSON.parse(args.list); | ||
| list = parse(args.list); | ||
| } catch { | ||
| return 0; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never write code like this. clever code is bad and unreadable
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it acceptable without the timeout?