瀏覽代碼

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

Ben Hsieh 7 年之前
父節點
當前提交
680331d68b
共有 2 個檔案被更改,包括 12 行新增14 行删除
  1. 10
    10
      polyfill/XMLHttpRequest.js
  2. 2
    4
      utils/uuid.js

+ 10
- 10
polyfill/XMLHttpRequest.js 查看文件

@@ -196,11 +196,11 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
196 196
                     })
197 197
                     .fetch(_method, _url, _headers, body)
198 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,7 +274,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
274 274
     return result.substr(0, result.length-2)
275 275
   }
276 276
 
277
-  _headerReceived(e) {
277
+  _headerReceived = (e) => {
278 278
     log.debug('header received ', this._task.taskId, e)
279 279
     this.responseURL = this._url
280 280
     if(e.state === "2") {
@@ -285,7 +285,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
285 285
     }
286 286
   }
287 287
 
288
-  _uploadProgressEvent(send:number, total:number) {
288
+  _uploadProgressEvent = (send:number, total:number) => {
289 289
     if(!this._uploadStarted) {
290 290
       this.upload.dispatchEvent('loadstart')
291 291
       this._uploadStarted = true
@@ -295,7 +295,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
295 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 299
     log.verbose(this.readyState)
300 300
     if(this._readyState === XMLHttpRequest.HEADERS_RECEIVED)
301 301
       this._dispatchReadStateChange(XMLHttpRequest.LOADING)
@@ -310,7 +310,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
310 310
     this.dispatchEvent('progress', e)
311 311
   }
312 312
 
313
-  _onError(err) {
313
+  _onError = (err) => {
314 314
     let statusCode = Math.floor(this.status)
315 315
     if(statusCode >= 100 && statusCode !== 408) {
316 316
       return
@@ -331,7 +331,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
331 331
     this.clearEventListeners()
332 332
   }
333 333
 
334
-  _onDone(resp) {
334
+  _onDone = (resp) => {
335 335
     log.debug('XMLHttpRequest done', this._url, resp, this)
336 336
     this._statusText = this._status
337 337
     let responseDataReady = () => {

+ 2
- 4
utils/uuid.js 查看文件

@@ -1,6 +1,4 @@
1 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
 }