简体   繁体   中英

Is there a more efficient way to populate a dropdown than a loop?

I've been improving page performance, but when it comes to loading the various dropdowns I am losing too much time. is there a quicker way to populate the controls than this?

my.service.data.controls is an in-memory collection of collections of json data from an ajax call for the various controls in key/value pairs (Id, Name), Days, Years, Salutations, etc

$.each(my.service.data.controls.Days, function (i, p) {
     days.push(new my.models.dropdown(selectedItem)
     .Id(p.Id)
     .Name(p.Id));
});

// for creating Position Models
my.models.dropdown = function (selectedItem) {
    var self = this;
    self.Id = ko.observable();
    self.Name = ko.observable();
    // non-persitable properties
    self.isSelected = ko.computed(function () {
        return selectedItem() === self;
    });
};

I recommend profiling your js (if you haven't already) before you try to optimize it. The performance pain may be originating somewhere else in your code.

To get started, pop open the Chrome inspector, select the 'profiles' tab, and click start. Then load your page, click stop, and see what's up!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM