3

I have some JavaScript benchmark code that is supported to be running on browser. But I would like to run it on console mode of JavaScript engine such as 'd8' in V8 for testing purpose.

I have written empty variables and functions for each DOM variables and functions(e.g. document.getElementById, etc.). But I cannot fully run the code since setTimeout() and setInterval() are supported by browser not from V8 engine. Is there a way to implement or simply emulate those functions in pure JavaScript code?

I appreciate any kind of comments.

Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
Honggyu Kim
  • 178
  • 1
  • 8
  • "and setInterval() are supported by browser not from V8 engine", **are** or **are not**? – Alexandr Lazarev Mar 06 '16 at 08:34
  • Possible duplicate of [How is asynchronous callback implemented?](http://stackoverflow.com/questions/23406584/how-is-asynchronous-callback-implemented) – GolezTrol Mar 06 '16 at 08:35
  • Could you use Node.js? Node includes setTimeout and setInterval functions which work much the same as in the browser. – user1751825 Mar 06 '16 at 08:36
  • I would like to run some code that includes setTimeout() and setInterval() in pure V8 engine without browser. Those are not supported by V8 engine. I'm looking for a way to emulate those functions in pure JavaScript code. – Honggyu Kim Mar 06 '16 at 08:37
  • @GolezTrol, Thanks for the link. It seems that I should write write another C++ code as a V8 embedder then implement setInterval JavaScript function in C++ code. I still want to just write those functions in pure JavaScript code so that I can easily run it in 'd8'. But that could be the last resort if I fail to find a way to write setTimeout in pure JavaScript code. Thanks a lot! – Honggyu Kim Mar 06 '16 at 08:44
  • @user1751825, Unfortunately, I cannot use node.js for my purpose. I can only use V8. But thank you for the comment! – Honggyu Kim Mar 06 '16 at 08:46
  • Note that `setTimeout` and `setInterval` are not part of the [ECMA-262](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf). The documentation of `V8` [states that](https://developers.google.com/v8/) *It implements ECMAScript as specified in ECMA-262, 3rd edition*. – skypjack Mar 06 '16 at 08:46
  • @skypjack, That's right. 'setTimeout' and 'setInterval' are not part of ECMA-262. I know it, but I would like to just simply emulate it to run some JS benchmarks. Thanks! – Honggyu Kim Mar 06 '16 at 08:54
  • see related answer - https://stackoverflow.com/q/50665051/104380 – vsync Jun 03 '18 at 09:48

2 Answers2

3

You can't to that with just V8 + some JS, you have to embbed the JS engine in some kind of runtime that supports timers. For V8 there's zombie.js for instance.

Ilya
  • 5,377
  • 2
  • 18
  • 33
  • Actually, I don't have to wait for some duration of time to run code in setTimeout. So it could be possible to implement setTimeout(), but more difficult part is to emulate setInterval(). Thanks for the link but I cannot use node.js for my purpose. I also appreciate your comment! – Honggyu Kim Mar 06 '16 at 08:49
  • My purpose is to evaulate JavaScript engine performance with a set of JS benchmarks such as octane, jsbench, enyobench, etc. I would like to only focus on JS performance without DOM access time. Running those benchmarks on console mode with 'd8' is much simpler to automate and analysis the V8 performance in my case. – Honggyu Kim Mar 06 '16 at 08:52
  • I think you're a bit trying to reinvent the wheel here. What's the point of implementing the missing functions in JS rather than in (say) C or C++ like node.js does ? – Ilya Mar 06 '16 at 09:02
  • "but more difficult part is to emulate setInterval()" => I'd say it's impossible in pure JS – Ilya Mar 06 '16 at 09:04
  • the reason is to keep the original source of V8, then measure the performance for each different versions. If I write those functions in C++, then I may have to add those code whenever I change V8 code version. If that's the only solution, I have to go for it as you said. And I also thought implementing setInterval could be impossible, but I asked here if there's a way that I could think of. Thanks for your comment! – Honggyu Kim Mar 06 '16 at 09:24
  • Well, you can implement a setInterval that does nothing, it would allow you to benchmark with the rest of the code. As you're only comparing the engines anyway, it could be acceptable to cripple the test code, depending on what it does. – Ilya Mar 06 '16 at 09:35
2

Let's take a look how it is implemented in node timers.js. You can see at line #24, that timer_wrap binding is registered. This "internal module" is just a C library that supplies js module with time operations. That means that js timer implementation is based on some modules of an "upper level" and unfortunately can't be achieved in "pure js" as you wish.

Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
  • 2
    I appreciate everyone's comment here. I may have to conclude my question as you said. It's not possible to implement or simply emulate setTimeout and setInterval functions in pure JavaScript code. I may have to write C++ code as a V8 embedded and I can get some reference node.js code as you showed me. I appreciate your comment! – Honggyu Kim Mar 06 '16 at 09:27