Over a million developers have joined DZone.

JS Object Optimization Bug in Chrome 52

A bug has appeared in Chrome 52 concerning Backbone and callbacks not firing. Have you come across it?

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Image title

That was on Friday. I still don’t know why the bug happens, but I can reproduce it. Evidence says it’s a Chrome 52 bug, not a general JavaScript bug. Neither Safari nor Firefox make it happen.

Sometimes when you call .fetch() on a Backbone model, callbacks don’t fire. There’s no JavaScript error, the REST call works, the result is vanished by the browser. Joy of joys.

2 fetches, 1 callback

You need:

  • Webpack
  • Babel 6
  • Backbone (maybe Backbone is doing it wrong?)
  • A server that can serve JSON files
  • 14 lines of code

I used create-react-app to set up a basic environment, ejected with npm run eject, and added some config to Webpack to allow jQuery. Backbone depends on it. This part is not that relevant.

To show the bug itself, we need a Backbone model:

// A basic Backbone model
class BugModel extends Backbone.Model {
    constructor() {

        this.url = "bla.json";

Now I know what you’re thinking, that’s an ES6 object and Backbone was built before ES6. Official docs never mention the new class syntax. The traditional Backbone approach works too:

var BugModel = Backbone.Model.extend({
    url: "bla.json"

The same bug happens. I tried.

To cause the bug, we have to call .fetch() on two instances of BugModel that share the same id. The code looks like this:

let bug = new BugModel();
    success: () => {
        console.log('fetch 1'); // this happens

function doWeirdness(bug) {
    let newBug = new BugModel({id: bug.id});

    console.log('about to re-fetch');

        success: () => console.log('fetch 2', newBug), // don't get here
        error: () => console.log('error')

We create an instance of BugModel in a variable called bug, then call .fetch(). This makes an API call to /bla.json, which returns a JSON file that looks like this: {"id": 1, "hai": "hello"}.

Backbone automagically parses this JSON string and sets an id and hai attribute. This part works like a charm.

Then we call doWeirdness and pass our bug object as an attribute. Inside doWeirdness, we make a new instance of BugModel called newBug and give it the same id as bug had. This is a crucial step.

When we call fetch on this new instance, the API call happens, but the callbacks do not. 'fetch 2' never prints.

This whole sequence of events might sound redundant, but it made sense in my real codebase. Even if re-fetching objects doesn’t make sense, it should work. It works in both Safari and Firefox.

Works in SafariWorks in Firefox

Clues About Why

This smells like a bug in memory optimization. Chrome thinks bug and newBug are the same object, even though they are not.

If you call fetch() twice on the same instance of BugModel, the same bug happens. (clue, part 1)

If both instances have a different id, both fetch calls fire callbacks. (clue, part 2)

Curiously, if you add a third fetch() call, that works.

        success: () => console.log('fetch 2', newBug), // don't get here
        error: () => console.log('error')

        success: () => console.log('fetch 3') // prints

The bug didn’t happen in Chrome 51.

I am confused.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

backbone ,error ,bug ,web ,javascript ,chrome ,object ,optimization

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}