Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/js/array.js

Issue 2775503006: [builtins] Improve performance of array.prototype.filter and map (Closed)
Patch Set: Remove old impl. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils, extrasUtils) { 5 (function(global, utils, extrasUtils) {
6 6
7 "use strict"; 7 "use strict";
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
(...skipping 983 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 994
995 995
996 function ArraySort(comparefn) { 996 function ArraySort(comparefn) {
997 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); 997 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
998 998
999 var array = TO_OBJECT(this); 999 var array = TO_OBJECT(this);
1000 var length = TO_LENGTH(array.length); 1000 var length = TO_LENGTH(array.length);
1001 return InnerArraySort(array, length, comparefn); 1001 return InnerArraySort(array, length, comparefn);
1002 } 1002 }
1003 1003
1004
1005 // The following functions cannot be made efficient on sparse arrays while
1006 // preserving the semantics, since the calls to the receiver function can add
1007 // or delete elements from the array.
1008 function InnerArrayFilter(f, receiver, array, length, result) {
1009 var result_length = 0;
1010 for (var i = 0; i < length; i++) {
1011 if (i in array) {
1012 var element = array[i];
1013 if (%_Call(f, receiver, element, i, array)) {
1014 %CreateDataProperty(result, result_length, element);
1015 result_length++;
1016 }
1017 }
1018 }
1019 return result;
1020 }
1021
1022
1023
1024 function ArrayFilter(f, receiver) {
1025 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
1026
1027 // Pull out the length so that modifications to the length in the
1028 // loop will not affect the looping and side effects are visible.
1029 var array = TO_OBJECT(this);
1030 var length = TO_LENGTH(array.length);
1031 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f);
1032 var result = ArraySpeciesCreate(array, 0);
1033 return InnerArrayFilter(f, receiver, array, length, result);
1034 }
1035
1036 function ArrayMap(f, receiver) {
1037 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
1038
1039 // Pull out the length so that modifications to the length in the
1040 // loop will not affect the looping and side effects are visible.
1041 var array = TO_OBJECT(this);
1042 var length = TO_LENGTH(array.length);
1043 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f);
1044 var result = ArraySpeciesCreate(array, length);
1045 for (var i = 0; i < length; i++) {
1046 if (i in array) {
1047 var element = array[i];
1048 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array));
1049 }
1050 }
1051 return result;
1052 }
1053
1054
danno 2017/04/03 15:35:27 Nice. I like it.
1055 function ArrayLastIndexOf(element, index) { 1004 function ArrayLastIndexOf(element, index) {
1056 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf"); 1005 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
1057 1006
1058 var array = this; 1007 var array = this;
1059 var length = TO_LENGTH(this.length); 1008 var length = TO_LENGTH(this.length);
1060 1009
1061 if (length == 0) return -1; 1010 if (length == 0) return -1;
1062 if (arguments.length < 2) { 1011 if (arguments.length < 2) {
1063 index = length - 1; 1012 index = length - 1;
1064 } else { 1013 } else {
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1375 "toLocaleString", getFunction("toLocaleString", ArrayToLocaleString), 1324 "toLocaleString", getFunction("toLocaleString", ArrayToLocaleString),
1376 "join", getFunction("join", ArrayJoin), 1325 "join", getFunction("join", ArrayJoin),
1377 "pop", getFunction("pop", ArrayPop), 1326 "pop", getFunction("pop", ArrayPop),
1378 "push", getFunction("push", ArrayPush, 1), 1327 "push", getFunction("push", ArrayPush, 1),
1379 "reverse", getFunction("reverse", ArrayReverse), 1328 "reverse", getFunction("reverse", ArrayReverse),
1380 "shift", getFunction("shift", ArrayShift), 1329 "shift", getFunction("shift", ArrayShift),
1381 "unshift", getFunction("unshift", ArrayUnshift, 1), 1330 "unshift", getFunction("unshift", ArrayUnshift, 1),
1382 "slice", getFunction("slice", ArraySlice, 2), 1331 "slice", getFunction("slice", ArraySlice, 2),
1383 "splice", getFunction("splice", ArraySplice, 2), 1332 "splice", getFunction("splice", ArraySplice, 2),
1384 "sort", getFunction("sort", ArraySort), 1333 "sort", getFunction("sort", ArraySort),
1385 "filter", getFunction("filter", ArrayFilter, 1),
1386 "map", getFunction("map", ArrayMap, 1),
1387 "indexOf", getFunction("indexOf", null, 1), 1334 "indexOf", getFunction("indexOf", null, 1),
1388 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1335 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1389 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2), 1336 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2),
1390 "find", getFunction("find", ArrayFind, 1), 1337 "find", getFunction("find", ArrayFind, 1),
1391 "findIndex", getFunction("findIndex", ArrayFindIndex, 1), 1338 "findIndex", getFunction("findIndex", ArrayFindIndex, 1),
1392 "fill", getFunction("fill", ArrayFill, 1), 1339 "fill", getFunction("fill", ArrayFill, 1),
1393 "includes", getFunction("includes", null, 1), 1340 "includes", getFunction("includes", null, 1),
1394 "entries", IteratorFunctions.entries, 1341 "entries", IteratorFunctions.entries,
1395 "keys", IteratorFunctions.keys, 1342 "keys", IteratorFunctions.keys,
1396 iteratorSymbol, IteratorFunctions.values 1343 iteratorSymbol, IteratorFunctions.values
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1433 1380
1434 // ------------------------------------------------------------------- 1381 // -------------------------------------------------------------------
1435 // Exports 1382 // Exports
1436 1383
1437 utils.Export(function(to) { 1384 utils.Export(function(to) {
1438 to.ArrayFrom = ArrayFrom; 1385 to.ArrayFrom = ArrayFrom;
1439 to.ArrayJoin = ArrayJoin; 1386 to.ArrayJoin = ArrayJoin;
1440 to.ArrayPush = ArrayPush; 1387 to.ArrayPush = ArrayPush;
1441 to.ArrayToString = ArrayToString; 1388 to.ArrayToString = ArrayToString;
1442 to.ArrayValues = IteratorFunctions.values, 1389 to.ArrayValues = IteratorFunctions.values,
1443 to.InnerArrayFilter = InnerArrayFilter;
1444 to.InnerArrayFind = InnerArrayFind; 1390 to.InnerArrayFind = InnerArrayFind;
1445 to.InnerArrayFindIndex = InnerArrayFindIndex; 1391 to.InnerArrayFindIndex = InnerArrayFindIndex;
1446 to.InnerArrayJoin = InnerArrayJoin; 1392 to.InnerArrayJoin = InnerArrayJoin;
1447 to.InnerArraySort = InnerArraySort; 1393 to.InnerArraySort = InnerArraySort;
1448 to.InnerArrayToLocaleString = InnerArrayToLocaleString; 1394 to.InnerArrayToLocaleString = InnerArrayToLocaleString;
1449 }); 1395 });
1450 1396
1451 %InstallToContext([ 1397 %InstallToContext([
1452 "array_entries_iterator", IteratorFunctions.entries, 1398 "array_entries_iterator", IteratorFunctions.entries,
1453 "array_keys_iterator", IteratorFunctions.keys, 1399 "array_keys_iterator", IteratorFunctions.keys,
1454 "array_pop", ArrayPop, 1400 "array_pop", ArrayPop,
1455 "array_push", ArrayPush, 1401 "array_push", ArrayPush,
1456 "array_shift", ArrayShift, 1402 "array_shift", ArrayShift,
1457 "array_splice", ArraySplice, 1403 "array_splice", ArraySplice,
1458 "array_slice", ArraySlice, 1404 "array_slice", ArraySlice,
1459 "array_unshift", ArrayUnshift, 1405 "array_unshift", ArrayUnshift,
1460 "array_values_iterator", IteratorFunctions.values, 1406 "array_values_iterator", IteratorFunctions.values,
1461 ]); 1407 ]);
1462 1408
1463 }); 1409 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698