Skip to content
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

Add support for color string in JSON array format #189

Closed
wants to merge 3 commits into from

Conversation

pramilk
Copy link
Contributor

@pramilk pramilk commented May 28, 2023

Please see discussion #187 for details

Launch Checklist

  • Confirm your changes do not include backports from Mapbox projects (unless with compliant license) - if you are not sure about this, please ask!
  • Briefly describe the changes in this PR.
  • Link to related issues.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality.
  • Document any changes to public APIs.
  • Post benchmark scores.
  • Add an entry to CHANGELOG.md under the ## main section.

clamp(+b / 255, 0, 1),
clamp(isNaN(a) ? 1 : +a, 0, 1)];
}
} catch(ex) {
Copy link
Contributor Author

@pramilk pramilk May 28, 2023

Choose a reason for hiding this comment

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

My understanding previously was that try/catch was slower. But looks like modern browsers have improved and now I don't see any perf issue with using try/catch (tested on Edge/Chrome/Firefox). Adding try/catch here allow to catch all invalid JSON string.

https://jsbench.me/mbli72txix
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think try-catch is slower compared to ifs, in case you go through the catch part of the try-catch, at least is what I was taught long time ago... :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if code goes inside catch then its quite slow. But for any production style, it should never go into catch block as it will be a invalid color and thus production code does not suffer from any performance degradation in this case. Overhead of adding various checks to validate JSON format will eat away any performance benefits apart from adding code complications.

https://jsbench.me/mbli72txix/2
image

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2023

Can you post benchmark results against regular color parsing (rgb(...) for example)?

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2023

cc: @louwers @ovivoda

@pramilk
Copy link
Contributor Author

pramilk commented May 28, 2023

Can you post benchmark results against regular color parsing (rgb(...) for example)?

Here are the results using the code used in this PR from my home machine. RGB, RGBA and HSL is 50-65% slower then JSON array (depending on machine).

https://jsbench.me/84li0k8nkg/1

image

@pramilk
Copy link
Contributor Author

pramilk commented May 30, 2023

As per discussion in #187, holding on to this PR (Moving to draft). Will first impliment color caching which will help improve color parsing by about 50%. If there is still need to further improve, will come back to this PR.

@pramilk pramilk marked this pull request as draft May 30, 2023 19:00
@HarelM
Copy link
Collaborator

HarelM commented Dec 27, 2023

@pramilk I'm closing this PR for now to reduce clutter.
If this is still relevant, please open it in the future.

@HarelM HarelM closed this Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants