npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ember-cli-sparse-array

v0.0.2

Published

A sparse array implementation to use in Ember.js projects.

Readme

EmberCLI Sparse Array Addon

An implementation of SparseArray for Ember.js configured as an addon so that you may use it in any of your EmberCLI projects.

Installation

ember install ember-cli-sparse-array
ember g emer-cli-sparse-array

This will create an addon namespace for your application, 'ember-cli-sparse-array'. Available in this namespace is a lib/sparse-array, which gives your code an array definition to work with.

Usage

import Ember from 'ember';
import SparseArray from 'ember-cli-sparse-array/lib/sparse-array';

export default SparseArray.extend({
    // Implement your `load` callback ;)
})

You can return the sparse array to a route's model hook (or afterModel or beforeModel depending on your needs) if you wish your route's controller's model to be the content of a lazy-loadable sparse array.

You can use the a route's ...controllerFor('my_route').set('model', mySparseArray); to do your bidding... You just neet do implement the load callback.

Instantiating a SparseArray

When instantiating a SparseArray, you have to supply a load method. The load method is responsible for fetching a subset of the resource, e.g. from your server.

The load method must accept two arguments:

  • offset: The 0-based index/offset of the first record to be fetched.
  • limit: How many records should be fetched

The load method must return a thenable (i.e. a promise or another object with a .then method on it) that eventually resolves with a hash with two keys:

  • total: The total number of records.
  • items: An array of maximum limit items that start at offset.

Here is an example:

var SparseArray = require('ember-cli-sparse-array/lib/sparse-array');

var comments = SparseArray.create({
    load: function(offset, limit) {
        return new Em.RSVP.Promise(function(resolve) {
            $.getJSON('/comments?offset='+offset+'&limit='+limit).then(function(payload) {
                resolve({
                    total: payload.meta.total,
                    items: payload.comments
                });
            });
        });
    }
});

The SparseArray will automatically call your load method when items at not-yet-loaded indexes are being requested by your application.

Using a SparseArray

The length attribute of your SparseArray works just like a normal array. It will start out as 0. It will be set to the value of total each time a promise from load resolves.

load is always called instantly with offset being 0 when you instantiate a SparseArray. This is to find the length property right away.

The SparseArray also has a property called isLoaded, which is false until the first time a load completes, where it will be set to true, and stay true forever.

Calling .objectAt at an index that's greater than or equals to the current length, will always return null.

Example:

var comments = SparseArray.create({
    load: function(offset, limit) {/* ... */}
});
comments.get('length'); //Will always be 0, since the data hasn't been loaded yet
comments.get('isLoaded'); //false
comments.objectAt(0); //null
comments.objectAt(9999999); //null

//Later, after the first load has completed:
comments.get('length'); //The `total` value that you resolved the promise with
comments.get('isLoaded'); //true
comments.objectAt(0); //Whatever you returned at the index 0

After the load method has completed at least once, and the array has a length property, requesting an index less than length, will make the array fetch the items around that index using the load method, if the index has not been loaded yet.

Notice about using {{each}} and other eager consumers

The {{each}} Handlebars helper is "eager", meaning that it will insert views for all items right away. So when your load promise resolves with a total value of 9000, it will create 9000 views right away, and request each of the 9000 items from your array. This may result in a lot of requests.

The real power of sparse arrays is when using them together with container views that only requests (i.e. calls .objectAt) items that is supposed to be in the browser's current viewport. A good example is Ember.ListView (disclaimer: This sparse array implementation has not been tested with Ember.ListView yet, but you get the idea).

Options

You can set other options than the load method when instantiating SparseArray. Example:

SparseArray.create({
    batchSize: 42,
    load: function() {/* ... */}
});

The following options are supported:

  • batchSize (integer): The maximum number of records the sparse array will ask the load method to load through the limit argument. The actual value of limit may be lower, since the sparse array will only load items that have not already been loaded. Defaults to 100.

Known limitations

  • Rejected promises from the load method are currently not handled.
  • Short lists (length of 1 or 2) are not the best use of this.
  • Lists where the last object is on its own "page" will never load the last page (because the objectAt logic is difficult).

Contriubte

Fork and PR :)

Above in "Known limitations" is a good place to start picking up issues.