Simplify _.flatten and add benchmarks.

Former-commit-id: f541328bf680a75abea68bce813820def375f4a0
This commit is contained in:
John-David Dalton
2012-05-23 02:19:35 -04:00
parent 26d9cc972e
commit 5315058e2d
3 changed files with 53 additions and 9 deletions

View File

@@ -976,9 +976,6 @@
* // => [1, 2, 3, [[4]]]; * // => [1, 2, 3, [[4]]];
*/ */
function flatten(array, shallow) { function flatten(array, shallow) {
if (shallow) {
return concat.apply(ArrayProto, array);
}
var value, var value,
index = -1, index = -1,
length = array.length, length = array.length,
@@ -987,7 +984,7 @@
while (++index < length) { while (++index < length) {
value = array[index]; value = array[index];
if (isArray(value)) { if (isArray(value)) {
push.apply(result, flatten(value)); push.apply(result, shallow ? value : flatten(value));
} else { } else {
result.push(value); result.push(value);
} }

View File

@@ -12,11 +12,11 @@
</style> </style>
</head> </head>
<body> <body>
<script src="../lodash.min.js"></script> <script src="../lodash.js"></script>
<script> <script>
var lodash = _.noConflict(); var lodash = _.noConflict();
</script> </script>
<script src="../vendor/underscore/underscore-min.js"></script> <script src="../vendor/underscore/underscore.js"></script>
<script src="../vendor/benchmark.js/benchmark.js"></script> <script src="../vendor/benchmark.js/benchmark.js"></script>
<script src="../vendor/firebug-lite/src/firebug-lite-debug.js"></script> <script src="../vendor/firebug-lite/src/firebug-lite-debug.js"></script>
<script> <script>

View File

@@ -48,7 +48,12 @@
_ = window._, _ = window._,
lodash = window.lodash, lodash = window.lodash,
numbers = [], numbers = [],
object = {}; object = {},
words = [
'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
'seventeen', 'eighteen', 'nineteen', 'twenty'
];
for (var index = 0; index < 20; index++) { for (var index = 0; index < 20; index++) {
numbers[index] = index; numbers[index] = index;
@@ -119,8 +124,6 @@
}) })
); );
/*--------------------------------------------------------------------------*/
suites.push( suites.push(
Benchmark.Suite('each object') Benchmark.Suite('each object')
.add('Lo-Dash', function() { .add('Lo-Dash', function() {
@@ -139,6 +142,50 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('flatten deep')
.add('Lo-Dash', function() {
lodash.flatten([1, [2], [3, [[4]]]]);
})
.add('Underscore', function() {
_.flatten([1, [2], [3, [[4]]]]);
})
);
suites.push(
Benchmark.Suite('flatten shallow')
.add('Lo-Dash', function() {
lodash.flatten([1, [2], [3, [[4]]]], true);
})
.add('Underscore', function() {
_.flatten([1, [2], [3, [[4]]]], true);
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('groupBy with callback')
.add('Lo-Dash', function() {
lodash.groupBy(numbers, function(num) { return Math.floor(num); });
})
.add('Underscore', function() {
_.groupBy(numbers, function(num) { return Math.floor(num); });
})
);
suites.push(
Benchmark.Suite('groupBy with property name')
.add('Lo-Dash', function() {
lodash.groupBy(words, 'length');
})
.add('Underscore', function() {
_.groupBy(words, 'length');
})
);
/*--------------------------------------------------------------------------*/
suites.push( suites.push(
Benchmark.Suite('keys') Benchmark.Suite('keys')
.add('Lo-Dash', function() { .add('Lo-Dash', function() {