Page tree
Skip to end of metadata
Go to start of metadata

When returning large/many FB::variant or FB::VariantMap objects to javascript, you may experience a bottleneck in the conversion when the data is passed back to the browser.

You would detect this occurring in cases where a method within your plugin reaches the end of its execution, but the return or end of execution of the calling javascript function does not occur until a great deal of time later.

A workaround to speed this up is to offload the parsing of the objects to the browser JSON parser (if one is available). The example below illustrates one way this could be done.

* You will need to have already done the required setup of jsoncpp in order to use follow this example; see FireBreath Libraries for instructions on setting up jsoncpp

#include "DOM/Window.h"

...

FB::variant yourPluginAPI::aMethodThatReturnsParsedVariants()
{

    ...
    creation of the VariantMap object here
    ...

    // Retrieve a reference to the DOM Window
    FB::DOM::WindowPtr window = m_host->getDOMWindow();

    // Check if the DOM Window has an in-built JSON Parser
    if (window && window->getJSObject()->HasProperty("JSON")) {
        // Convert the VariantMap to a Json::Value object
        Json::Value json_map = FB::variantToJsonValue(users_variant_map);

        // Create a writer that will convert the object to a string
        Json::FastWriter writer;

        // Create a reference to the browsers JSON utility
        FB::JSObjectPtr obj = window->getProperty<FB::JSObjectPtr>("JSON");

        // Return the result of the invocation of JSON.parse
        return obj->Invoke("parse", FB::variant_list_of(writer.write(json_map)));
    } else {
        // No browser JSON parser detected, falling back to return of an FB::variant object
        return users_variant_map;
    }
}

...

Note: That example omits plugin setup and/or any form of error checking, and is only to describe the process.

Performance Testing

Here are the results from a comparison test for returing objects parsed by the method above, versus just returning the variant object(s):

from the Chromium console, I executed the following set of tests where "aMethodThatReturnsVariants" does not use the JSON.parse method, and "aMethodThatReturnsParsedVariants" uses the browsers JSON.parse method.

Test 1.1; not using JSON.parse

This method was setup to return a very large/complex FB::VariantMap -

var start = new Date().getTime(); myplugin.aMethodThatReturnsVariants; var end = new Date().getTime(); console.log("Execution time for return of VariantMap:", end - start);

Result:
Execution time for return of VariantMap: 5688

Test 1.2; using JSON.parse

This method was setup to return the same large/complex FB::VariantMap as before, but parsed using JSON.parse -

var start = new Date().getTime(); myplugin.aMethodThatReturnsParsedVariants; var end = new Date().getTime(); console.log("Execution time for return of VariantMap object parsed with JSON.parse:", end - start);

Result:
Execution time for return of VariantMap object parsed with JSON.parse: 727

 

Test 2.1; not using JSON.parse

Next I setup a test to make many calls to the same function, with the only difference being that the returned FB::VariantMap is much smaller and less complex -

var start = new Date().getTime(); for (i = 0; i < 100; ++i) { myplugin.aMethodThatReturnsVariants }; var end = new Date().getTime(); console.log("Execution time for return of 100 small VariantMap objects:", end - start);

Result:
Execution time for return of 100 small VariantMap objects: 4756

Test 2.2; using JSON.parse

The same test as before, 

var start = new Date().getTime(); for (i = 0; i < 100; ++i) { myplugin.aMethodThatReturnsParsedVariants }; var end = new Date().getTime(); console.log("Execution time for return of 100 smaller VariantMap objects parsed with JSON.parse:", end - start);

Result:
Execution time for return of 100 smaller VariantMap objects parsed with JSON.parse: 2637

As you can see, in both cases the return of an FB::variant object that had been parsed by the JSON parser is remarkably faster when measured at the return of the calling javascript function.

  • No labels