Javascript code for making my browser slow down

后端 未结 8 1827
野性不改
野性不改 2021-02-19 02:28

I\'m writing a library for WebWorkers, and I want to test the difference between running a script in the main page thread, versus in one or more workers. The problem is: I can\'

8条回答
  •  离开以前
    2021-02-19 02:50

    /**
     * Block CPU for the given amount of seconds
     * @param {Number} [seconds]
     */
    function slowdown(seconds = 0.5) {
      const start = (new Date()).getTime()
      let end = start
      while (end - start < seconds * 1000) {
        end = (new Date()).getTime()
      }
    }
    

    Calling this method will slow code down for the given amount of seconds (with ~200ms precision).

提交回复
热议问题