Last active
August 29, 2015 14:06
-
-
Save zkat/1930f5b34e2aae56b82c to your computer and use it in GitHub Desktop.
playing around with auto-filtered/mapped `can.List`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
var sourceList = new can.List(); | |
var stream = sourceList.filteredStream(function(todo, map) { | |
if (!todo.attr("done")) { | |
todo.attr("steps").forEach(map); | |
} | |
}); | |
var targetList = stream.toList(); | |
sourceList.push({ | |
done: false, | |
description: "Do the Bitovi Dance", | |
steps: ["hop around", "do a little twirl"] | |
}); | |
console.log(targetList.attr()); // ["hop around", "do a little twirl"] | |
sourceList.push({ | |
done: true, | |
description: "Assert opinion", | |
steps: ["clear throat", "push up glasses", "raise index finger", "mansplain"] | |
}); | |
console.log(targetList.attr()); // ["hop around", "do a little twirl"] | |
sourceList.push({ | |
done: false, | |
description: "Do some open source", | |
steps: ["Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
}); | |
console.log(targetList.attr()); // ["hop around", "do a little twirl", "Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
sourceList.attr(0).attr("steps").pop(); | |
console.log(targetList.attr()); // ["hop around", "Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
sourceList.attr(0).attr("done", true); | |
console.log(targetList.attr()); // ["Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
sourceList.shift(); | |
console.log(targetList.attr()); // ["Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
sourceList.shift(); | |
console.log(targetList.attr()); // ["Download Emacs.app", "grow a neckbeard", "make a PR on github"] | |
sourceList.shift(); | |
console.log(targetList.attr()); // [] | |
console.log("Benchmarking..."); | |
console.time("building"); | |
for (var i = 0; i < 100; i++) { | |
sourceList.push({ | |
done: false, | |
description: "Do the Bitovi Dance", | |
steps: ["hop around", "do a little twirl"] | |
}); | |
} | |
console.timeEnd("building"); | |
console.log("target list length: ", targetList.length); | |
console.time("done"); | |
sourceList.forEach(function(todo) { todo.attr("done", true); }); | |
console.timeEnd("done"); | |
console.time("todo"); | |
sourceList.forEach(function(todo) { todo.attr("done", false); }); | |
console.timeEnd("todo"); | |
console.time("removing"); | |
for (var i = 0; i < sourceList.length; i++) { | |
sourceList.pop(); | |
} | |
console.timeEnd("removing"); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment