deyles-zz / sculejs

SculeJS - data structures for the web
165 stars 24 forks source link

Performance when using SculeJS on older iPhones and Android #55

Open john-dalsgaard opened 8 years ago

john-dalsgaard commented 8 years ago

Do you have any experience with performance on iOS 7 (as on an iPhone 4) and Android 4.4.x?

It seems SculeJS takes a very long time to load...

And I have another issue on a tester's phone (so I cannot connect it directly to my computer) where writing 31,000 small records to a collection seems to be where the app just "stops" (or freezes). Have you seen anything like this? Any ideas how to debug and possibly improve?

deyles-zz commented 8 years ago

Hey John,

That's weird - I remember back when I first released SculeJS I was testing on an iPhone 3G and was able to work with hundreds of thousands of documents at the same time. A few things to check:

The best way I can think of debugging the issue is profiling each section of code in the app to try and figure out where things are slowing down (or where the crash occurs). If you're timing a loop that inserts documents and it gets progressively slower then there might be something about that particular code that is causing the issue (leaking references to objects, a variable being reset in global scope somewhere, etc).

If you're certain the performance bottleneck isn't in the application code then I'd start digging into the SculeJS source and add some profiling calls there. Inserting documents should be a fairly trivial operation for Scule - they'll be added to the collection and indexed via primary key. It might be worth testing to see if the user's phone has similar issues adding the same number of POJOs to an array.

Let me know how you get on, I don't have much time to write code at the moment but I'm happy to have you bounce ideas off me if you get stuck.

Cheers, Dan

john-dalsgaard commented 8 years ago

Make sure the app is running in a single execution context. If there are multiple JS interpreters running with their own copies of the database in memory the handset might be struggling to handle the load

I am not sure what you mean by this? Is that something I have to set deliberately?

The way I load it is like this:

/lib/data/factory.js

var Alloy = require('alloy'), Backbone = require('alloy/backbone'), _ = require('alloy/underscore')._;
var scule  = require('com.scule');
var password = 'xxxxx';
var protocol = 'scule+titanium://';
scule.debug(true);

// Return meta data collections
exports.version = scule.factoryCollection(protocol+'version', {secret:password});
exports.syncStatus = scule.factoryCollection(protocol+'syncStatus', {secret:password});
exports.boasts = scule.factoryCollection(protocol+'boasts', {secret:password});
:
:

And then in my controllers I do something like this:

var DataFactory = require('data/factory');
var users = DataFactory.users;

function getUser(crit){
    var user = null;
    var crit = (crit) ? crit : {};
    if(users.getLength() > 0) {
        var result = users.find(crit, {$limit:1, $sort:{lastLogin:-1}});
        user = result[0];
    }
    return user;
};

function updateLastLogin(user) {
    if(user){
        var xUser = null;
       if(user.key && user.key !== ''){
            xUser = getUser({key:user.key});
        }
        xUser['lastLogin'] = new Date();
        users.save(xUser);
        users.commit();
    }
};

I would think that was in line with what I have seen in the documentation - but please correct my if I am wrong!

As for the way I update the datasets/collections I think it is Ok. For bulk updates (e.g. sync with service) I typically traverse the data returned from the Ajax call, find the record, and do whatever manipulation is needed and do a collection.save(record) for each. And then a commit when all work is done.

I have tested on an iPhone 4 - which is not quick, I know. I did some jQuery Mobile development on the same device 3-4 years ago - and found it was slow (i.e. Javascript in the browser) - which led me to ask you. The problems on loading in Android are even bigger.... I am not absolutely sure this is due to SculeJS - but combined with the previous experience about Javascript I thought I'ld ask you.

Do you have any suggestions as to how I can get closer to determining whether SculeJS is the culprit or not? Is there any debugging I can enable - or some performance metrics I can dig into?