jsbench-me icon indicating copy to clipboard operation
jsbench-me copied to clipboard

Cannot use inline scripts in setup HTML

Open mitar opened this issue 5 years ago • 4 comments

I am trying to use the following code in setup HTML:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bson.bundle.js" crossorigin="anonymous"></script>
<script>const MongoBSON = BSON;</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bson.js" crossorigin="anonymous"></script>
<script>const Bsonfy = BSON;</script>

But it seems this is not possible and that this tool modifies the injected code and tries to load <script src(unknown)> instead?

mitar avatar Oct 30 '20 05:10 mitar

I just looked into this. It does seem like a bug that inline SCRIPT fails to transform into sandbox HTML test code properly, with SRC(unknown). I just fixed that but then realized that proper thing to do might be completely the opposite ... to disable inline script in setup HTML. Why?

Because I have a feeling it's a wrong way to use SetupHTML in benchmark.js. My idea of this field is that it serves 2 purposes only:

  1. inject EXTERNAL javascript
  2. add HTML to BODY so test possibly manipulates this HTML.

... for inline Javascript, you should use SetupJS box instead. So, in your case, you should just move 2 lines to SetupJS:

const MongoBSON = BSON;
const Bsonfy = BSON;

... and it should work as expected. Declaring constants in SetupHTML leads to other problems too, like error message "variable X is already declared" when you rerun the tests (because SetupHTML reruns too).

Please let me know if there is some usage scenario I'm missing. Right now I'm in doubt if I should leave it as it was or completely disable inline SCRIPT in SetupHTML.

psiho avatar Apr 28 '21 11:04 psiho

Colsing this for no activity. At the end I decided to completely remove inline scripts as SetupJS seems like a better place for them.

psiho avatar May 01 '21 20:05 psiho

So the issue here is that both of those sourced scripts expose a global BSON symbol. And this is why I have to store them into a custom variable, so that I can use them later on in the benchmark. Doing:

const MongoBSON = BSON;
const Bsonfy = BSON;

Would make both variables be the same implementation. So in my use case (which I think it is common) I want to compare multiple libraries providing the same/compatible symbol and see how they perform. Both of those libraries export BSON and how do I then compare those implementations?

mitar avatar May 11 '21 20:05 mitar

Haven't thought of that use case. I'll patch this as soon as I find time and release minor update

psiho avatar May 23 '21 20:05 psiho