Browse Source

Optimize JS performance by using different UID generator and remove bind statements

Ben Hsieh 7 years ago
parent
commit
680331d68b
2 changed files with 12 additions and 14 deletions
  1. 10
    10
      polyfill/XMLHttpRequest.js
  2. 2
    4
      utils/uuid.js

+ 10
- 10
polyfill/XMLHttpRequest.js View File

196
                     })
196
                     })
197
                     .fetch(_method, _url, _headers, body)
197
                     .fetch(_method, _url, _headers, body)
198
       this._task
198
       this._task
199
-          .stateChange(this._headerReceived.bind(this))
200
-          .uploadProgress(this._uploadProgressEvent.bind(this))
201
-          .progress(this._progressEvent.bind(this))
202
-          .catch(this._onError.bind(this))
203
-          .then(this._onDone.bind(this))
199
+          .stateChange(this._headerReceived)
200
+          .uploadProgress(this._uploadProgressEvent)
201
+          .progress(this._progressEvent)
202
+          .catch(this._onError)
203
+          .then(this._onDone)
204
 
204
 
205
     })
205
     })
206
   }
206
   }
274
     return result.substr(0, result.length-2)
274
     return result.substr(0, result.length-2)
275
   }
275
   }
276
 
276
 
277
-  _headerReceived(e) {
277
+  _headerReceived = (e) => {
278
     log.debug('header received ', this._task.taskId, e)
278
     log.debug('header received ', this._task.taskId, e)
279
     this.responseURL = this._url
279
     this.responseURL = this._url
280
     if(e.state === "2") {
280
     if(e.state === "2") {
285
     }
285
     }
286
   }
286
   }
287
 
287
 
288
-  _uploadProgressEvent(send:number, total:number) {
288
+  _uploadProgressEvent = (send:number, total:number) => {
289
     if(!this._uploadStarted) {
289
     if(!this._uploadStarted) {
290
       this.upload.dispatchEvent('loadstart')
290
       this.upload.dispatchEvent('loadstart')
291
       this._uploadStarted = true
291
       this._uploadStarted = true
295
     this.upload.dispatchEvent('progress', new ProgressEvent(true, send, total))
295
     this.upload.dispatchEvent('progress', new ProgressEvent(true, send, total))
296
   }
296
   }
297
 
297
 
298
-  _progressEvent(send:number, total:number, chunk:string) {
298
+  _progressEvent = (send:number, total:number, chunk:string) => {
299
     log.verbose(this.readyState)
299
     log.verbose(this.readyState)
300
     if(this._readyState === XMLHttpRequest.HEADERS_RECEIVED)
300
     if(this._readyState === XMLHttpRequest.HEADERS_RECEIVED)
301
       this._dispatchReadStateChange(XMLHttpRequest.LOADING)
301
       this._dispatchReadStateChange(XMLHttpRequest.LOADING)
310
     this.dispatchEvent('progress', e)
310
     this.dispatchEvent('progress', e)
311
   }
311
   }
312
 
312
 
313
-  _onError(err) {
313
+  _onError = (err) => {
314
     let statusCode = Math.floor(this.status)
314
     let statusCode = Math.floor(this.status)
315
     if(statusCode >= 100 && statusCode !== 408) {
315
     if(statusCode >= 100 && statusCode !== 408) {
316
       return
316
       return
331
     this.clearEventListeners()
331
     this.clearEventListeners()
332
   }
332
   }
333
 
333
 
334
-  _onDone(resp) {
334
+  _onDone = (resp) => {
335
     log.debug('XMLHttpRequest done', this._url, resp, this)
335
     log.debug('XMLHttpRequest done', this._url, resp, this)
336
     this._statusText = this._status
336
     this._statusText = this._status
337
     let responseDataReady = () => {
337
     let responseDataReady = () => {

+ 2
- 4
utils/uuid.js View File

1
 export default function getUUID() {
1
 export default function getUUID() {
2
-  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
3
-    let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
4
-    return v.toString(16);
5
-  });
2
+  return Math.random().toString(36).substring(2, 15) +
3
+        Math.random().toString(36).substring(2, 15);
6
 }
4
 }