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

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

Issue 2775503006: [builtins] Improve performance of array.prototype.filter and map (Closed)
Patch Set: Code comments 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
« no previous file with comments | « src/code-stub-assembler.cc ('k') | src/js/typedarray.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 982 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 993
994 994
995 function ArraySort(comparefn) { 995 function ArraySort(comparefn) {
996 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); 996 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
997 997
998 var array = TO_OBJECT(this); 998 var array = TO_OBJECT(this);
999 var length = TO_LENGTH(array.length); 999 var length = TO_LENGTH(array.length);
1000 return InnerArraySort(array, length, comparefn); 1000 return InnerArraySort(array, length, comparefn);
1001 } 1001 }
1002 1002
1003
1004 // The following functions cannot be made efficient on sparse arrays while
1005 // preserving the semantics, since the calls to the receiver function can add
1006 // or delete elements from the array.
1007 function InnerArrayFilter(f, receiver, array, length, result) {
1008 var result_length = 0;
1009 for (var i = 0; i < length; i++) {
1010 if (i in array) {
1011 var element = array[i];
1012 if (%_Call(f, receiver, element, i, array)) {
1013 %CreateDataProperty(result, result_length, element);
1014 result_length++;
1015 }
1016 }
1017 }
1018 return result;
1019 }
1020
1021
1022
1023 function ArrayFilter(f, receiver) {
1024 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
1025
1026 // Pull out the length so that modifications to the length in the
1027 // loop will not affect the looping and side effects are visible.
1028 var array = TO_OBJECT(this);
1029 var length = TO_LENGTH(array.length);
1030 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f);
1031 var result = ArraySpeciesCreate(array, 0);
1032 return InnerArrayFilter(f, receiver, array, length, result);
1033 }
1034
1035 function ArrayMap(f, receiver) {
1036 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
1037
1038 // Pull out the length so that modifications to the length in the
1039 // loop will not affect the looping and side effects are visible.
1040 var array = TO_OBJECT(this);
1041 var length = TO_LENGTH(array.length);
1042 if (!IS_CALLABLE(f)) throw %make_type_error(kCalledNonCallable, f);
1043 var result = ArraySpeciesCreate(array, length);
1044 for (var i = 0; i < length; i++) {
1045 if (i in array) {
1046 var element = array[i];
1047 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array));
1048 }
1049 }
1050 return result;
1051 }
1052
1053
1054 function ArrayLastIndexOf(element, index) { 1003 function ArrayLastIndexOf(element, index) {
1055 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf"); 1004 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
1056 1005
1057 var array = this; 1006 var array = this;
1058 var length = TO_LENGTH(this.length); 1007 var length = TO_LENGTH(this.length);
1059 1008
1060 if (length == 0) return -1; 1009 if (length == 0) return -1;
1061 if (arguments.length < 2) { 1010 if (arguments.length < 2) {
1062 index = length - 1; 1011 index = length - 1;
1063 } else { 1012 } else {
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 "toLocaleString", getFunction("toLocaleString", ArrayToLocaleString), 1323 "toLocaleString", getFunction("toLocaleString", ArrayToLocaleString),
1375 "join", getFunction("join", ArrayJoin), 1324 "join", getFunction("join", ArrayJoin),
1376 "pop", getFunction("pop", ArrayPop), 1325 "pop", getFunction("pop", ArrayPop),
1377 "push", getFunction("push", ArrayPush, 1), 1326 "push", getFunction("push", ArrayPush, 1),
1378 "reverse", getFunction("reverse", ArrayReverse), 1327 "reverse", getFunction("reverse", ArrayReverse),
1379 "shift", getFunction("shift", ArrayShift), 1328 "shift", getFunction("shift", ArrayShift),
1380 "unshift", getFunction("unshift", ArrayUnshift, 1), 1329 "unshift", getFunction("unshift", ArrayUnshift, 1),
1381 "slice", getFunction("slice", ArraySlice, 2), 1330 "slice", getFunction("slice", ArraySlice, 2),
1382 "splice", getFunction("splice", ArraySplice, 2), 1331 "splice", getFunction("splice", ArraySplice, 2),
1383 "sort", getFunction("sort", ArraySort), 1332 "sort", getFunction("sort", ArraySort),
1384 "filter", getFunction("filter", ArrayFilter, 1),
1385 "map", getFunction("map", ArrayMap, 1),
1386 "indexOf", getFunction("indexOf", null, 1), 1333 "indexOf", getFunction("indexOf", null, 1),
1387 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1334 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1388 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2), 1335 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2),
1389 "find", getFunction("find", ArrayFind, 1), 1336 "find", getFunction("find", ArrayFind, 1),
1390 "findIndex", getFunction("findIndex", ArrayFindIndex, 1), 1337 "findIndex", getFunction("findIndex", ArrayFindIndex, 1),
1391 "fill", getFunction("fill", ArrayFill, 1), 1338 "fill", getFunction("fill", ArrayFill, 1),
1392 "includes", getFunction("includes", null, 1), 1339 "includes", getFunction("includes", null, 1),
1393 "entries", IteratorFunctions.entries, 1340 "entries", IteratorFunctions.entries,
1394 "keys", IteratorFunctions.keys, 1341 "keys", IteratorFunctions.keys,
1395 iteratorSymbol, IteratorFunctions.values 1342 iteratorSymbol, IteratorFunctions.values
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1432 1379
1433 // ------------------------------------------------------------------- 1380 // -------------------------------------------------------------------
1434 // Exports 1381 // Exports
1435 1382
1436 utils.Export(function(to) { 1383 utils.Export(function(to) {
1437 to.ArrayFrom = ArrayFrom; 1384 to.ArrayFrom = ArrayFrom;
1438 to.ArrayJoin = ArrayJoin; 1385 to.ArrayJoin = ArrayJoin;
1439 to.ArrayPush = ArrayPush; 1386 to.ArrayPush = ArrayPush;
1440 to.ArrayToString = ArrayToString; 1387 to.ArrayToString = ArrayToString;
1441 to.ArrayValues = IteratorFunctions.values, 1388 to.ArrayValues = IteratorFunctions.values,
1442 to.InnerArrayFilter = InnerArrayFilter;
1443 to.InnerArrayFind = InnerArrayFind; 1389 to.InnerArrayFind = InnerArrayFind;
1444 to.InnerArrayFindIndex = InnerArrayFindIndex; 1390 to.InnerArrayFindIndex = InnerArrayFindIndex;
1445 to.InnerArrayJoin = InnerArrayJoin; 1391 to.InnerArrayJoin = InnerArrayJoin;
1446 to.InnerArraySort = InnerArraySort; 1392 to.InnerArraySort = InnerArraySort;
1447 to.InnerArrayToLocaleString = InnerArrayToLocaleString; 1393 to.InnerArrayToLocaleString = InnerArrayToLocaleString;
1448 }); 1394 });
1449 1395
1450 %InstallToContext([ 1396 %InstallToContext([
1451 "array_entries_iterator", IteratorFunctions.entries, 1397 "array_entries_iterator", IteratorFunctions.entries,
1452 "array_keys_iterator", IteratorFunctions.keys, 1398 "array_keys_iterator", IteratorFunctions.keys,
1453 "array_pop", ArrayPop, 1399 "array_pop", ArrayPop,
1454 "array_push", ArrayPush, 1400 "array_push", ArrayPush,
1455 "array_shift", ArrayShift, 1401 "array_shift", ArrayShift,
1456 "array_splice", ArraySplice, 1402 "array_splice", ArraySplice,
1457 "array_slice", ArraySlice, 1403 "array_slice", ArraySlice,
1458 "array_unshift", ArrayUnshift, 1404 "array_unshift", ArrayUnshift,
1459 "array_values_iterator", IteratorFunctions.values, 1405 "array_values_iterator", IteratorFunctions.values,
1460 ]); 1406 ]);
1461 1407
1462 }); 1408 });
OLDNEW
« no previous file with comments | « src/code-stub-assembler.cc ('k') | src/js/typedarray.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698