Skip to content

Skyhight173/json: add cache to improve performance#2457

Closed
LegoBrainBiker wants to merge 10 commits into
TurboWarp:masterfrom
LegoBrainBiker:patch-4
Closed

Skyhight173/json: add cache to improve performance#2457
LegoBrainBiker wants to merge 10 commits into
TurboWarp:masterfrom
LegoBrainBiker:patch-4

Conversation

@LegoBrainBiker

Copy link
Copy Markdown
Contributor

the json extension can be slow because it needs to parse and stringify each time a block is called. this pr adds a cache so that if the json was recently created by the project, it can be looked up without using JSON.parse.

@github-actions github-actions Bot added the pr: change existing extension Pull requests that change an existing extension label Apr 9, 2026
@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

!format

@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

!format

Comment thread extensions/Skyhigh173/json.js Outdated
const objectCache = {};
function parse(string) {
return (
objectCache[string] ??

Copy link
Copy Markdown
Member

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

@LegoBrainBiker LegoBrainBiker Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was referring to objectCache[string] ?? (objectCache[string] = JSON.parse(string)); which is very indicipherable to normal people

Comment thread extensions/Skyhigh173/json.js Outdated
@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

!format

Comment thread extensions/Skyhigh173/json.js
@LegoBrainBiker LegoBrainBiker requested a review from a team as a code owner May 16, 2026 17:31
@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

!format

@Xeltalliv

Copy link
Copy Markdown
Contributor

I don't think this should be merged.

Caching isn't always good, and in some cases people may want to avoid it. If people do want caching:

This extension is currently unique precisely because it does not do caching.

The main concern with caching is that it increases RAM usage. Especially with implementation like this, if project does a lot of operations synchronously (within "run without screen refresh" custom block), it may quite easy to run out of RAM and crash the browser tab on projects that previously ran fine.

@SharkPool-SP

Copy link
Copy Markdown
Collaborator

I gotta agree with Xeltalliv here, this should be closed in favor of the other JSON extension PRs

@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

I don't think this should be merged.

Caching isn't always good, and in some cases people may want to avoid it. If people do want caching:

This extension is currently unique precisely because it does not do caching.

The main concern with caching is that it increases RAM usage. Especially with implementation like this, if project does a lot of operations synchronously (within "run without screen refresh" custom block), it may quite easy to run out of RAM and crash the browser tab on projects that previously ran fine.

anything that would use a significant amount of memory already wouldn't run "fine" the current implementation runs very slowly with
turbo-refrence.js
repeated operations. the caches are also cleared often, so the memory impact aren't that insignificant.

I personally don't use the JSON extension, instead using my own object extension. I agree that the json extension is not useful in most projects, and could be replaced, but that's no reason not to make the existing json extension the best it can be.

@SharkPool-SP

Copy link
Copy Markdown
Collaborator

The only problem with changing the existing extension is that it would have to implement breaking changes

@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

The only problem with changing the existing extension is that it would have to implement breaking changes

this pull request has no breaking changes, to a user, it would work exactly the same but faster.

@SharkPool-SP

Copy link
Copy Markdown
Collaborator

The only problem with changing the existing extension is that it would have to implement breaking changes

this pull request has no breaking changes, to a user, it would work exactly the same but faster.

The problem is it would only work faster for preexisting objects that are the exact same. Additionally it would hog RAM space like previously mentioned

@SharkPool-SP

Copy link
Copy Markdown
Collaborator

So there's not much speed gain for regular usage.

@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

The only problem with changing the existing extension is that it would have to implement breaking changes

this pull request has no breaking changes, to a user, it would work exactly the same but faster.

The problem is it would only work faster for preexisting objects that are the exact same. Additionally it would hog RAM space like previously mentioned

preexisting objects normally need to be reparsed whenever multiple items of an array/object are read. additionally, if an existing array or object is modified or indexed, my changes also cache those, so if for example if you want to apply multiple operations to an object or array, it would only have to parse the initial object or array, not each time an item is modified. I cannot think of a single scenario which this would not speed up.

@SharkPool-SP

Copy link
Copy Markdown
Collaborator

Yes but that quickly builds up in ram. And in those cases it's typically a one-time-use

@LegoBrainBiker

Copy link
Copy Markdown
Contributor Author

Yes but that quickly builds up in ram. And in those cases it's typically a one-time-use

it only lasts for a short time.

@GarboMuffin

Copy link
Copy Markdown
Member

usually yes but not necessarily always

for example, a run w/o screen refresh would never be clearing its cache with the extension as-written

there's some ways you could address that. a max size cap in some way (probably try to account for big JSON objects and such) might make it tolerable w/o making things slower even in the worst case

i'd be chill with that kind of idea, done well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: change existing extension Pull requests that change an existing extension

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants