           i = 0, keys, l;

            if (Array.isArray(obj)) {
                for (l = obj.length; i < l; i++) {
                    dispose(obj[i]);
                }
            } else if (viewModelPrototype.isPrototypeOf(obj)) {
                for (keys = Object.keys(obj), l = keys.length; i < l; i++) {
                    dispose(obj[keys[i]]);
                }
                if (obj.dispose) {
                    obj.dispose();
                }
                if (syncSpy) {
                    syncSpy.disposed(obj);
                }
            }
        }

        function syncRequire(metadata, metadataIndices, rootModel, rootViewModel, callback) {

            if (!(Array.isArray(metadata) &&
                Array.isArray(metadataIndices) &&
                rootModel instanceof Object)) {
                throw new Error('sync called with invalid arguments');
            }
            if (!(viewModelPrototype.isPrototypeOf(rootViewModel) || callback instanceof Function)) {
                throw new Error('sync requires an existing ViewModel or a callback');
            }

            var moduleNames = [],
                templateNames = [],
                loadTime = Date.now();

            metadataIndices.forEach(function (i) {

                var meta = metadata[i],
                    moduleName = meta.module,
                    templateName = meta.template;

                if (moduleName && moduleNames.indexOf(moduleName) === -1) {
                    moduleNames.push(moduleName);
                }
                if (templateName && templateNames.indexOf(templateName) === -1) {
                    templateNames.push(templateName);
                }
            });

            console.assert(syncCount === 1, 'syncRequire called again before update completion');

            require(moduleNames, function syncUpdate() {

                var syncTime = Date.now(),
                    modules = arguments,
                    afterSyncEvent;

                console.assert(syncCount === 1, 'syncUpdate called out of order');

                try {

                    tmpl(templateNames);

                    loadTime = Date.now() - loadTime;

                    if (syncSpy) {
                        syncSpy.start(rootViewModel);
                    }

                    function moduleLookup(moduleName) {
                        var index = moduleNames.indexOf(moduleName);
                        return index > -1 ? modules[index] : null;
                    }

                    function getViewModel(model) {

                        if (Array.isArray(model)) {
                            return model.map(function (childModel) {
                                return getViewModel(childModel);
                            });
                        }

                        if (model instanceof Object) {

                            var meta = metadata[model.i] || {},
                                module = moduleLookup(meta.module),
                                prototype = module && viewModelPrototype.isPrototypeOf(module.prototype) ?
                                    module.prototype : viewModelPrototype,
                                observables = meta.observable || [],
                                configurables = meta.configurable || [],
                                enumerables = meta.enumerable || [],
                                writables = meta.writable || [],
                                viewModel = Object.create(prototype),
                                descriptors = {};

                            delete model.i;

                            if (meta.template) {
                                descriptors.template = {
                                    value: ko.observable(meta.template),
                                    enumerable: true
                                };
                            }

                            Object.keys(model).forEach(function (key) {

                                if (observables.indexOf(key) > -1) {

                                    var childViewModel = getViewModel(model[key]);

                                    if (utils.isArrayWithUniqueIds(childViewModel)) {
                                        descriptors[key] = {
                                            value: ko.observableArray(childViewModel),
                                            enumerable: true
                                        };
                                    } else {

                                        if (syncSpy && syncSpy.ids && Array.isArray(childViewModel)) {
                                            syncSpy.creatingObservableContainingArray(model, key);
                                        }

                                        descriptors[key] = {
                                            value: ko.observable(childViewModel).model(),
                                            enumerable: true
                                        };
                                    }
                                } else {
                                    descriptors[key] = {
                                        value: model[key],
                                        configurable: configurables.indexOf(key) > -1,
                                        enumerable: enumerables.indexOf(key) > -1,
                                        writable: writables.indexOf(key) > -1
                                    };
                                }
                            });

                            Object.defineProperties(viewModel, descriptors);

                            if (module && module.customizer instanceof Function) {
                                module.customizer(viewModel, model);
                            }

                            Object.seal(viewModel);

                            if (syncSpy) {
                                syncSpy.created(viewModel);
                            }

                            return viewModel;
                        }

                        return model;
                    }

                    function updateObservableArray(observableArray, viewModels, models) {

                        var mutated = false,
                            modelIds = models.map(function (model) {
                                return model.id;
                            }),
                            viewModelsToRemove = viewModels.filter(function (viewModel) {
                                return modelIds.indexOf(viewModel.id) < 0;
                            }),
                            modelsToAdd = models.filter(function (model) {
                                return !viewModels.some(function (viewModel) {
                                    return viewModel.id === model.id;
                                });
                            });

                        function willMutate() {
                            if (!mutated) {
                                observableArray.valueWillMutate();
                            }
                            mutated = true;
                        }

                        if (viewModelsToRemove.length) {
                            willMutate();
                            viewModelsToRemove.forEach(function (viewModel) {
                                dispose(viewModel);
                                viewModels.splice(viewModels.indexOf(viewModel), 1);
                            });
                        }

                        viewModels.forEach(function (viewModel) {
                            var model = models[modelIds.indexOf(viewModel.id)];
                            if (viewModelPrototype.isPrototypeOf(viewModel)) {
                                updateObservables(viewModel, model);
                            }
                        });

                        if (modelsToAdd.length) {
                            willMutate();
                            Array.prototype.push.apply(viewModels, modelsToAdd.map(function (model) {
                                return getViewModel(model);
                            }));
                        }

                        if (syncSpy && syncSpy.ids) {
                            syncSpy.validateSortableArrays(viewModels, models);
                        }

                        if (
                            viewModels.length === models.length &&
                            viewModels.some(function (viewModel, index) {
                            return viewModel.id !== models[index].id;
                        })) {
                            willMutate();
                            viewModels.sort(function (left, right) {
                                return modelIds.indexOf(left.id) - modelIds.indexOf(right.id);
                            });
                        }

                        if (mutated) {
                            observableArray.valueHasMutated();
                        }
                        return mutated;
                    }

                    function updateObservables(viewModel, model) {

                        Object.keys(viewModel).forEach(function (key) {

                            var observable = viewModel[key],
                                currentValue,
                                newValue,
                                childViewModel;

                            if (ko.isObservable(observable) && model.hasOwnProperty(key)) {
                                currentValue = observable.peek();
                                newValue = model[key];
                                if (currentValue !== newValue) {
                                    if (Array.isArray(currentValue) && Array.isArray(newValue)) {

                                        if (utils.isObservableArray(observable) &&
                                            utils.isArrayWithUniqueIds(newValue)) {
                                            updateObservableArray(observable, currentValue, newValue);
                                        } else if (utils.arraysDiffer(currentValue, newValue)) {

                                            if (syncSpy && syncSpy.ids) {
                                                syncSpy.replacingArrayInsideObservable(viewModel, model, key);
                                            }

                                            dispose(currentValue);
                                            childViewModel = getViewModel(newValue);
                                            observable.model(childViewModel);
                                        }
                                    } else {
                                        if (newValue == null) {
                                            dispose(currentValue);
                                            observable.model(null);
                                        } else if (viewModelPrototype.isPrototypeOf(currentValue) &&
                                            utils.matchingIds(currentValue, newValue)) {
                                            updateObservables(currentValue, newValue);
                                        } else {

                                            if (syncSpy && syncSpy.ids &&
                                                viewModelPrototype.isPrototypeOf(currentValue)) {
                                                syncSpy.replacingViewModel(viewModel, key, newValue);
                                            }

                                            dispose(currentValue);
                                            childViewModel = getViewModel(newValue);
                                            observable.model(childViewModel);
                                        }
                                    }
                                }
                            }
                        });
                    }

                    if (viewModelPrototype.isPrototypeOf(rootViewModel)) {
                        updateObservables(rootViewModel, rootModel);
                    } else {
                        rootViewModel = getViewModel(rootModel);
                    }

                    if (syncSpy) {
                        syncSpy.end(rootViewModel);
                    }
                } catch (e) {

                    console.error('Error during sync: %o', e);

                } finally {

                    afterSyncEvent = document.createEvent("Events");
                    afterSyncEvent.initEvent("afterSync", true, false);
                    document.dispatchEvent(afterSyncEvent);

                    if (callback instanceof Function) {
                        callback(rootViewModel, { loadTime: loadTime, syncTime: Date.now() - syncTime });
                    }
                }
            });
        }

        function syncNext() {

            if (syncCount === 0 && queue.length > 0) {

                syncCount++;
                queue.shift()();
            }
        }

        function sync(metadata, metadataIndices, rootModel, rootViewModel, callback) {

            queue.push(function () {

                syncRequire(metadata, metadataIndices, rootModel, rootViewModel, function () {

                    if (callback instanceof Function) {
                        callback.apply(this, arguments);
                    }

                    syncCount--;
                    syncNext();
                });
            });

            syncNext();
        }

        return sync;
    });