Math.xxx >50x slower in userscripts
See original GitHub issueIssue
calling Math.abs, Math.random, etc. is >50x slower in userscripts
test
to test just copy the following into a userscript open the site it is injected into, and paste the same into the console:
let acc = 0;
console.time("Math.randomx1000000");
for(let i=0; i<1000000; i++) acc += Math.random();
console.timeEnd("Math.randomx1000000");
console.time("Math.absx1000000");
for(let i=0; i<1000000; i++) acc += Math.abs(acc);
console.timeEnd("Math.absx1000000");
// result in userscript:
// Math.randomx1000000: 4118.552978515625 ms
// Math.absx1000000: 4082.463134765625 ms
// result in console:
// Math.randomx1000000: 135.682861328125 ms
// Math.absx1000000: 55.900634765625 ms
temporary fix
rebinding the object like this fixes the problem:
const Math = window.Math;
despite the simple fix, this caused me major issues (freezes when doing image manipulation) and headaches (took me way to long to debug, because the profiler just shows 40s spent with Math.abs being the only thing on that line)
speculation on the origin
I assume the property access somehow has overhead because of sand-boxing or something, but the Math object is indistinguishable from the normal one (no proxy, no funky getters / setters), so I have no clue what is going on.
Issue Analytics
- State:
- Created a year ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Untitled
Gr 5 math problems, Sawan aaya hai song photo, Impressive resume samples for ... Basic skills english level 1, Atlantica internacional 2014, Slow...
Read more >OyM - River Thames Conditions
Correias gates agricolas, Koehring 1466 for sale, Top slow dance songs 70's, ... White picture frame 50 x 40, 6 month olds temperature,...
Read more >Psychtoolbox News
The new math fixes this, to deal with Retina displays better. Maybe restore backwards compatibility of Psychtoolbox 3.0.18 with macOS versions ...
Read more >Computer performance evaluation users group (CPEUG)
Applied Mathematics — Electronics and Electrical Engineering^ — Mechanical ... XXX BAD REPLACE - WRONG RECORD ... times as slow as the equivalent...
Read more >Advanced Bash-Scripting Guide
This is also the division arithmetic operator . ` command substitution The `command` construct makes available the output of command for assignment to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve implemented both tricks in #1532.
As for adaptive caching, I doubt it’s trivial so I’m afraid it’ll unnecessarily complicate code.
Just an Idea: you could count how many times each property gets used in the windowProxy and then dynamicly enable option 2 for everything where the overhead would be greater than 100ms on the next run.