dtao /lazy.js

Like Underscore, but lazier

★ 5,960⑂ 257👁 108⑃ 57● 178▤ 0
JavaScriptMITCreated 2013-05-10danieltao.com/lazy.js/
+0
Daily
5,960
stars
257
forks
108
watchers
57
prs
178
issues
0
releases

README

Like Underscore, but lazier
===========================





Lazy.js is a functional utility library for JavaScript, similar to Underscore and Lodash, but with a lazy engine under the hood that strives to do as little work as possible while being as flexible as possible.

It has no external dependencies, so you can get started right away with:

javascript
npm install lazy.js


(Note the package is called "lazy.js", with a dot.)

Or, if you're using Lazy.js in the browser:

html






Now let's look at what you can do with Lazy.js. (For more thorough information, take a look at the API Docs.)

Introduction
------------

Let's start with an array of objects representing people.

javascript
var people = getBigArrayOfPeople();


Suppose we're using this array to back some sort of search-as-you-type functionality, where users can search for people by their last names. Naturally we want to put some reasonable constraints on our problem space, so we'll provide up to 5 results at a time. Supposing the user types "Smith", we could therefore fetch results using something like this (using Underscore):

javascript
var results = _.chain(people)
  .pluck('lastName')
  .filter(function(name) { return name.startsWith('Smith'); })
  .take(5)
  .value();


This query does a lot of stuff:

- pluck('lastName'): iterates over the array and creates a new (potentially giant) array
- filter(...): iterates over the new array, creating yet another (potentially giant) array
- take(5): all that just for 5 elements!

So if performance and/or efficiency were a concern for you, you would probably not do things that way using Underscore. Instead, you'd likely go the procedural route:

javascript
var results = [];
for (var i = 0; i  sourceElement.clientWidth / 2; })
  .each(function(pos) { displayCoordinates(rightElement, pos); });


Anything else? Of course!

 String processing

Now here's something you may not have even thought of: String.match and String.split. In JavaScript, each of these methods returns an array of substrings. If you think about it, this often means doing more work than necessary; but it's the quickest way (from a developer's standpoint) to get the job done.

For example, suppose you wanted the first five lines of a block of text. You could always do this:

javascript
var firstFiveLines = text.split("\n").slice(0, 5);


But of course, this actually splits the entire string into every single line. If the string is very large, this is quite wasteful.

With Lazy.js, we don't need to split up an entire string just to treat it as a sequence of lines. We can get the same effect by wrapping the string with Lazy and calling split:

javascript
var firstFiveLines = Lazy(text).split("\n").take(5);


This way we can read the first five lines of an arbitrarily large string (without pre-populating a huge array) and map/reduce on it just as with any other sequence.

Similarly with String.match: let's say we wanted to find the first 5 alphanumeric matches in a string. With

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON