Subject: jquery each vs for loop前端
在看前端代碼的時候,看到很多地方都有用到jQuery.each() 方法,我昨天寫的reuse lib enhancement的時候也有用到它,好奇jQuery.each() 和原生JS的for loop效率,找到一個對比,看起來原生for loop執行效率要高更多,因而我就refactor 爲了原生for loop ?jquery
出處: https://jsperf.com/browser-diet-jquery-each-vs-for-loop
[圖片]jsp
sap.cus.crm.lib.reuse.controls.Note.prototype._getDefaultNoteTypeId = function() { var defaultNoteTypeId = "", noteTypes = this.getModel().getProperty(this.getProperty("noteTypes")); // second iteration with for loops // console.time('start of for loop with caching'); for (var i = 0, len = noteTypes.length; i < len; i ++) { if (noteTypes[i]["DefaultNoteType"]) { defaultNoteTypeId = noteTypes[i]["TextObjectID"]; return defaultNoteTypeId; } } // console.timeEnd('end of for loop'); /* first iteration with jQuery.each() jQuery.each(noteTypes, jQuery.proxy(function(index){ var sPrefix = this.getProperty("noteTypes") + "/" + index; // /NoteTypes/0 var isDefault = this.getModel().getProperty(sPrefix + "/" + this.getProperty("noteTypeDefaultFlag")); // /NoteTypes/0/DefaultNoteType if (isDefault) { defaultNoteTypeId = this.getModel().getProperty(sPrefix + "/" + this.getProperty("noteTypeId")); return false; } }, this)); */ // return defaultNoteTypeId; };