4

I have a SO question I am dying to know the answer to. However, I am concerned if it is on topic. The question asks about an unexplainable anomily in the performance of the await operator in the Javascript language on the browser of Chrome. Would this be an okay question to ask on Stack Overflow? (below is a preview of the full question.)

While testing the performance of await, I uncovered a confounding mystery. I ran each of the following code snippets several times each in the console to filter out flukes, and took the average times of the relevant data.

(function(console){
  "use strict";
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      O[0] = O_0;
    }
  console.timeEnd();
})(console);

Resulting in: default: 5.322021484375ms

Next, I tried adding making it asynchronous

(async function(console){
  "use strict";
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      O[0] = O_0;
    }
  console.timeEnd();
})(console);

Nice! Chrome knows its stuff. Very low overhead: default: 8.712890625ms

Next, I tried adding await.

(async function(console){
  "use strict";
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      O[0] = await O_0;
    }
  console.timeEnd();
})(console);

WHAT!?!?!?! NEAR 100x SPEED REDUCTION?!?!?! default: 724.706787109375ms

So, there must be some logical reason, right? I tried comparing the types prior.

(async function(console){
  "use strict";
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      O[0] = typeof O_0 === "object" ? await O_0 : O_0;
    }
  console.timeEnd();
})(console);

Okay, so that is not it: default: 6.7939453125ms

So then, it must be the promise-part: checking to see if the item passed to await is a promise. That must be the culprit, am I right or am I right?

(async function(console, Promise){
  "use strict";
  const isPromise = Promise.prototype.isPrototypeOf.bind(Promise);
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      O[0] = isPromise(O_0) ? await O_0 : O_0;
    }
  console.timeEnd();
})(console, Promise);

WHAT?!?!?!?!..... default: 7.2041015625ms

Okay, okay, let us give Chrome the benefit of the doubt. Let us assume, for a second, that they programmed await far less than perfectly.

(async function(console, Promise){
  "use strict";
  const isPromise = Promise.prototype.isPrototypeOf.bind(Promise);
  console.time();
  var O = [1];
  for (var i=0; i !== 107000; ++i) {
      const O_0 = O[0];
      const isAnObject = typeof O_0 === "object" ? true : false;
      const isPromise = isPromise(O_0);
      O[0] = isAnObject && isPromise ? await O_0 : O_0;
  }
  console.timeEnd();
})(console, Promise);

But even this fails to explain the poor performance of await: default: 7.85498046875ms

Okay, honestly, I give up. await should be at least 100x faster than it is now. There is not a single good reason why not. How? How? How is it this slow? Is there any hope of it being any faster in the future (like maybe, say, around about 100x faster)? I am looking for facts and an objective analysis of this issue that would explain the puzzling mystery I am seeing in the above performance tests.

Update: It has been posted now.

4
  • 1
    I think your third code block is incorrect. There is no await in there. Did you accidentally put the second block in twice?
    – david
    Commented Sep 7, 2018 at 2:02
  • 6
    Consider editing out "horror story" style before posting... Otherwise looks fine as answers say. Commented Sep 7, 2018 at 6:48
  • @David Thank you for noticing that. It appears that I did indeed forget to add in the await into the second block.
    – Jack G
    Commented Sep 7, 2018 at 11:24
  • @AlexeiLevenkov Thank you for your suggestion. I very much appreciate your constructive feedback, so I have replaced with the "horror story" style with a much more subdued "mystery puzzle" theme. I believe your suggestion makes the question flow much better.
    – Jack G
    Commented Sep 7, 2018 at 11:31

3 Answers 3

10

Revise your last paragraph by removing the first sentence, and blend that into the previous paragraph. Otherwise it's fine to post.

3
  • 9
    "Fine" is quite the understatement given the sort of questions we see on a daily basis, especially ones related to performance.
    – BoltClock
    Commented Sep 7, 2018 at 4:24
  • 2
    @BoltClock: I find it quite difficult to give accolades to questions. Must be just the way I'm wired.
    – Makoto
    Commented Sep 7, 2018 at 4:30
  • 7
    I mean, I see where you're coming from. This is the sort of standard we expect from questions, it ticks all the boxes, is answerable, etc. Now, if only it were the standard we'd see in what actually gets posted...
    – BoltClock
    Commented Sep 7, 2018 at 4:35
5

I see no reason that you couldn't post that question. Go ahead and post it.

2

Well, this advice comes late because you've already posted, but I think it is still important.

What you have is not a question. You are presenting your analysis, and your conclusions, which you pronounce as complete truth. You're not asking from input from other experts, you're trying to share your wisdom with the community.

Such teaching (or ranting) does not belong in a SO question. You could put teaching in an answer, or ranting on your blog, etc.

The culprit is this:

await should be at least 100x faster than it is now. There is not a single good reason why not.

If someone does know a good reason, why would they want to share it with you? You're already calling them a liar. (They might resort to gentle mockery of your tone)

Replace this pronouncement with a question, so your question doesn't read as if you are the arbiter of Javascript performance truth, and you'll be fine.

1
  • Thank you so much for your response. Sorry if I misworded that part. What I meant is neither that I am right nor that are no reasons, but rather that in a perfect world where efficiency were king and W3 was not lazy, await would be 100x faster. However, I do recognize that we do not live in a perfect world, so my question is really asking about how the world we live in deviates from that perfect world. And, i am still trying neither to expect nor to force upon any concept of such a perfect world onto out present day world because I do accept imperfect at the cost of reasoning why.
    – Jack G
    Commented Sep 9, 2018 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .