Giter Club home page Giter Club logo

knockout.wrap's Introduction

Knockout wrap

Motivation

The performance of ko.mapping.fromJS(JSObject) is very slow. A simple test wrapping 500 simple elements takes 700ms in firefox. This seems to be a known issue, that is still there.

Futhermore I don't have the need for ko.mappings ability to find out what needs to be updated in already mapped objects. This shaves off a huge chunk of the complexity.

Solution

A new plugin knockout.wrap with almost the same interface as ko.mapping (fromJS, fromJSON, toJS and toJSON). The plugin can do simple mappings from JS to observables and the other way again. Because the code is much simpler it is super fast:

ko.mapping.fromJS(500JSObjects): 737ms
ko.wrap.fromJS(500JSObjects): 16ms

ko.mapping.toJS(500WrappedObjects): 22ms
ko.wrap.toJS(500WrappedObjects): 5ms 

Usage

Basic use cases:

var observable = ko.wrap.fromJS(JSObjects);

var jsObjects = ko.wrap.toJS(observable);

Please note that if you are updating an observable, then it's preferable to use ko.wrap.updateFromJS(observable, jsObject).

ko.mapping can attach computed functions while it is wrapping. ko.wrap can do this as well, the syntax is a bit different:

function populateArray()
{
    var t = {elements: []};

    for (var i = 0; i < 500; ++i)
       t.elements.push({id: i, name: "hello" + i});

    return t;
}

var computedFunctions = {
    "/elements": function(e) {
        e.nameLength = ko.computed(function() {
            return e.name().length;
        }, e);
        return e;
    }
};

ko.wrap.fromJS(t, computedFunctions);

License

MIT license - http://www.opensource.org/licenses/mit-license.php

knockout.wrap's People

Contributors

arj03 avatar bago avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.