Skip to content

Commit 3f57975

Browse files
committedFeb 17, 2017
Use performance-now instead of custom solution
1 parent a9ad38a commit 3f57975

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"mime-types": "~2.1.7",
3939
"node-uuid": "~1.4.7",
4040
"oauth-sign": "~0.8.1",
41+
"performance-now": "^0.2.0",
4142
"qs": "~6.3.0",
4243
"stringstream": "~0.0.4",
4344
"tough-cookie": "~2.3.0",

‎request.js

+6-28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var http = require('http')
2828
, Multipart = require('./lib/multipart').Multipart
2929
, Redirect = require('./lib/redirect').Redirect
3030
, Tunnel = require('./lib/tunnel').Tunnel
31+
, now = require('performance-now')
3132

3233
var safeStringify = helpers.safeStringify
3334
, isReadStream = helpers.isReadStream
@@ -36,7 +37,6 @@ var safeStringify = helpers.safeStringify
3637
, copy = helpers.copy
3738
, version = helpers.version
3839
, globalCookieJar = cookies.jar()
39-
, hrTimeStart
4040

4141

4242
var globalPool = {}
@@ -92,28 +92,6 @@ function responseToJSON() {
9292
}
9393
}
9494

95-
function getHrTime() {
96-
if (typeof process === 'undefined' || !process.hrtime) {
97-
return 0
98-
}
99-
100-
var hr = process.hrtime()
101-
// convert to nanoseconds
102-
return hr[0] * 1e9 + hr[1]
103-
}
104-
105-
hrTimeStart = getHrTime()
106-
107-
function getTimeFromStart() {
108-
// in the browser, use performance.now()
109-
if (typeof performance !== 'undefined' && performance.now) {
110-
return performance.now()
111-
}
112-
113-
// in nodejs, use process.hrtime() (converting back to milliseconds)
114-
return (getHrTime() - hrTimeStart) / 1e6
115-
}
116-
11795
function Request (options) {
11896
// if given the method property in options, set property explicitMethod to true
11997

@@ -739,7 +717,7 @@ Request.prototype.start = function () {
739717
var self = this
740718

741719
if (self.timing) {
742-
var startTime = getTimeFromStart()
720+
var startTime = now()
743721
}
744722

745723
if (self._aborted) {
@@ -799,9 +777,9 @@ Request.prototype.start = function () {
799777
})
800778
self.req.on('socket', function(socket) {
801779
if (self.timing) {
802-
self.timings.socket = getTimeFromStart()
780+
self.timings.socket = now()
803781
socket.on('connect', function() {
804-
self.timings.connect = getTimeFromStart()
782+
self.timings.connect = now()
805783
})
806784
}
807785

@@ -888,13 +866,13 @@ Request.prototype.onRequestResponse = function (response) {
888866
var self = this
889867

890868
if (self.timing) {
891-
self.timings.response = getTimeFromStart()
869+
self.timings.response = now()
892870
}
893871

894872
debug('onRequestResponse', self.uri.href, response.statusCode, response.headers)
895873
response.on('end', function() {
896874
if (self.timing) {
897-
self.timings.end = getTimeFromStart()
875+
self.timings.end = now()
898876

899877
self.timings.dns = self.timings.socket - self.timings.start
900878
self.timings.tcp = self.timings.connect - self.timings.socket

0 commit comments

Comments
 (0)
Please sign in to comment.