-3

What I am trying to do is set a limit on how much processing power my script can use in javascript. For instance, after 2 MB it starts to lag similar to how NES games would lag when too much action is being displayed on the screen.

I have tried doing research, but nothing is giving me the answer I'm looking for. Everything is about improving performance as opposed to reducing the amount that can be used.

  • 1
    A browser usually gives as much memory and CPU resources as possible to scripts that it is running. I doubt you can artificially restrict that, much less from within the script. – Bergi Jan 30 '19 at 19:57
  • 1
    There's no way of setting a piece of script to be limited to a specific percent of CPU usage. You would have to create a wrapper around the browser and use some kind of governing code to limit that process' use of CPU. See [How can I programmatically limit my program's CPU usage to below 70%?](https://stackoverflow.com/q/989281/215552), for examples. – Heretic Monkey Jan 30 '19 at 19:58
  • _"after 2 MB it starts to lag"_. This statement suggests you've done some profiling of your code running on a browser. I think a more appropriate way to address the issue might be sharing the problematic code so we can provide suggestions for improving its performance and/or memory usage. – Patrick Roberts Jan 30 '19 at 21:05
  • I'm TRYING to make it lag after 2 MB, but thanks for the offer. –  Jan 30 '19 at 21:07
  • If you want to restrict memory usage to 2MB, you might consider using WebAssembly (and writing your program in some other language) – Bergi Jan 30 '19 at 21:12

1 Answers1

1

Since it seems you are trying to limit memory usage you might be able to track whatever it is that is using memory and simply limit that for example if you're creating a lot of objects you could reuse created objects once you created a certain number wich would limit used space.

As an asside I would suggest you check out service workers they might present an alternate way of solving your issue.