2

How do I properly do object detection for window.performance.timing?

An unknown version of Chrome being run by Googlebot is spawning the following error:

Uncaught TypeError: Cannot read property 'timing' of undefined

The only instances of window.performance.timing are in the following snippets of code:

else if (
window.performance!=undefined
&& window.performance.timing!=undefined
&& window.performance.timing.toJSON!=undefined) {/* etc */}

Obviously regardless of my efforts to do object detection Googlebot is somehow still spawning the error message. I can not use try and catch and I have zero instances in my JavaScript error log of this happening in any testable (e.g. Chrome itself) browser, only Googlebot.

John
  • 1
  • 13
  • 98
  • 177
  • Have you tried taking out the != undefined part, and just doing "else if (window.performance && window.performance.timing && " etc.? – John D. Oct 24 '15 at 00:23
  • This code works as expected: https://jsfiddle.net/x3373vm5/ . If `window.performance` is `undefined`, it would not try to access `window.performance.timing`. The error must originate from somewhere else. – Felix Kling Oct 24 '15 at 00:44
  • The problem is probably in `/* etc */`. :) The error message usually tells you the line number in your code. – Thevs Oct 25 '15 at 17:53
  • By the way, if it's a bot - have you tried to check the `window` object? – Thevs Oct 25 '15 at 17:56
  • @FelixKling - The first line of your jsfiddle sets `window.performance = undefined`... If that line is removed, the test fails - https://jsfiddle.net/v7zvvqoq/1/ – dana Nov 01 '15 at 23:29
  • @dana: Not sure what you mean with "the test fails". The point of my comment was that **if** `window.performance` is `undefined`, then one would not get the error message `Uncaught TypeError: Cannot read property 'timing' of undefined`. I.e. the OP couldn't possibly observe the mentioned error with that code. – Felix Kling Nov 01 '15 at 23:39
  • @FelixKling - Ya, I am not sure how this is being observed either. My point is there is a subtle difference between setting a variable to `undefined` and not declaring it at all. – dana Nov 01 '15 at 23:48
  • @dana: But this is about *properties*. For `window.performance!=undefined` there is no difference whether the property was explicitly set to `undefined` or doesn't exist. – Felix Kling Nov 02 '15 at 00:16
  • Well, this sheds some light on things for me - https://developer.mozilla.org/en-US/docs/Web/API/Performance/timing - I don't think I have an answer, but apparently there is an API for measuring browser load times? – dana Nov 02 '15 at 00:43
  • @dana http://caniuse.com/#search=performance and the HTML5rocks article looks simple enough. I don't use frameworks so I don't have to worry about performance myself. ;-) – John Nov 03 '15 at 11:53

3 Answers3

0

Try this way.

else if (
window.performance
&& window.performance.timing
&& window.performance.timing.toJSON) {/* etc */}

And as I know, you should use !== instead of !=

mehmet mecek
  • 2,615
  • 2
  • 21
  • 25
  • Can you also provide an explanation for why you think this solves the OP's problem? "Try this way" is not really useful information. – Felix Kling Oct 24 '15 at 00:36
  • I can't cause I don't know exactly why but my js developer friends always does it so. May be you may check and explain it here.. – mehmet mecek Oct 24 '15 at 00:41
  • 1
    No offense, but if you don't know what the problem is / could be, you should probably not write an answer. – Felix Kling Oct 24 '15 at 00:42
0

If window.performanceis undefined the if statement will still check the other two expressions. If there is no window.performance it can't check for window.performance.timing.

At least that's what I am guessing. I would try to nest the if statements:

if(window.performance != undefined) {
  if(window.performance.timing != undefined) {
    if(window.performance.timing.toJSON != undefined) {
      /* etc */
    }
  }
}
Brakebein
  • 2,197
  • 1
  • 16
  • 21
  • *"If window.performanceis undefined the if statement will still check the other two expressions."* No it won't. Your code is equivalent to theirs. – Felix Kling Oct 24 '15 at 00:35
  • I didn't down-vote this, was having dinner. This would be a good method of trial and error (I can pass parameters to my own error reporting function at each level). Got a bit of work to do. – John Oct 24 '15 at 01:20
-1

What if window.performance is null? I bet you would get the same error message.

That's why the canonical way to check objects existence is:

if (window.performance && window.performance.whatever && ...) ...

Thevs
  • 3,189
  • 2
  • 20
  • 32
  • *"What if window.performance is null? I bet you would get the same error message."* No. The error message would be `Uncaught TypeError: Cannot read property 'bar' of null`. But even if the value was `null`, `window.performance != undefined` will take care of `undefined` and `null` (because loose comparison). – Felix Kling Nov 02 '15 at 00:18