@samitouri / QOS-React-2 / commits / c41e86c990

Make ReactDefaultPerf work server-side

We were reading from window which was throwing when ReactDefaultPerf was injected.

Pete Hunt committed Aug 30, 2013 at 13:15 UTC c41e86c990efbd4b6c8315bb105381079eceb45a
2 files changed +47 -14
src/test/ReactDefaultPerf.js
+5 -14
@@ -19,6 +19,8 @@
19
20 "use strict";
21
22 +var performanceNow = require('performanceNow');
23 +
24 var ReactDefaultPerf = {};
25
26 if (__DEV__) {
@@ -186,9 +188,9 @@ if (__DEV__) {
188 var fnArgs = _getFnArguments(func);
189
190 return function() {
189 - var timeBeforeFn = now();
191 + var timeBeforeFn = performanceNow();
192 var fnReturn = func.apply(this, arguments);
191 - var timeAfterFn = now();
193 + var timeAfterFn = performanceNow();
194
195 /**
196 * Hold onto arguments in a readable way: args[1] -> args.component.
@@ -224,7 +226,7 @@ if (__DEV__) {
226 var callback = _getCallback(objName, fnName);
227 callback && callback(this, args, fnReturn, log, info);
228
227 - log.timing.timeToLog = now() - timeAfterFn;
229 + log.timing.timeToLog = performanceNow() - timeAfterFn;
230
231 return fnReturn;
232 };
@@ -400,17 +402,6 @@ if (__DEV__) {
402 var _microTime = function(time) {
403 return Math.round(time * 1000) / 1000;
404 };
403 -
404 - /**
405 - * Shim window.performance.now
406 - * We can't assign window.performance.now and then call it, so need to bind.
407 - * TODO: Support Firefox < 15 for now
408 - */
409 - var performance = window && (window.performance || window.webkitPeformance);
410 - if (!performance || !performance.now) {
411 - performance = Date;
412 - }
413 - var now = performance.now.bind(performance);
405 }
406
407 module.exports = ReactDefaultPerf;
src/test/performanceNow.js new
+42
@@ -0,0 +1,42 @@
1 +/**
2 + * Copyright 2013 Facebook, Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + * @providesModule performanceNow
17 + * @typechecks static-only
18 + */
19 +
20 +"use strict";
21 +
22 +var ExecutionEnvironment = require('ExecutionEnvironment');
23 +
24 +/**
25 + * Detect if we can use window.performance.now() and gracefully
26 + * fallback to Date.now() if it doesn't exist.
27 + * We need to support Firefox < 15 for now due to Facebook's webdriver
28 + * infrastructure.
29 + */
30 +var performance = null;
31 +
32 +if (ExecutionEnvironment.canUseDOM) {
33 + performance = window.performance || window.webkitPerformance;
34 +}
35 +
36 +if (!performance || !performance.now) {
37 + performance = Date;
38 +}
39 +
40 +var performanceNow = performance.now.bind(performance);
41 +
42 +module.exports = performanceNow;