js no for loop & 100 Array & Uint8Array & Typed Arrays All In One
js no for loop & 100 Array & Uint8Array & Typed Arrays All In One
js no for loop generate 100 length array
https://www.cnblogs.com/xgqfrms/p/8982974.html
// string arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1).map((item, i) => `` + item);
// (100) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
// number arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1);
// (100) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
// string + number
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1).map((item, i) => i % 2 === 0 ? item : item + ``);
// (100) [1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19, "20", 21, "22", 23, "24", 25, "26", 27, "28", 29, "30", 31, "32", 33, "34", 35, "36", 37, "38", 39, "40", 41, "42", 43, "44", 45, "46", 47, "48", 49, "50", 51, "52", 53, "54", 55, "56", 57, "58", 59, "60", 61, "62", 63, "64", 65, "66", 67, "68", 69, "70", 71, "72", 73, "74", 75, "76", 77, "78", 79, "80", 81, "82", 83, "84", 85, "86", 87, "88", 89, "90", 91, "92", 93, "94", 95, "96", 97, "98", 99, "100"]
Array.fill
const arr = new Array(100).fill(0).map((item, index) => index);
// no need new keyword
const arr = Array(100).fill(0).map((item, index) => index);
https://www.cnblogs.com/xgqfrms/tag/Array.fill
padStart
const arr = [...``.padStart(100, ` `)].map((item, i) => item = i+1);
const arr = ``;
const result = [...arr.padStart(100, "x")].map((x, i) => x = i+1);
console.log(`result =`, result);
// Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
bad
function generate100Array() {
var arr = new Array(100);
for(var i = 0; i < 100; i++){
arr[i]='';
}
return arr;
}
var a = generate100Array(),
b = generate100Array();
console.time('for');
for (var i = 0; i < a.length; i++) {
a[i] = i;
}
console.timeEnd('for');
console.time('forEach');
b.forEach(function (el, index) {
b[index] = index;
});
console.timeEnd('forEach');
for
is better thanforEach
map
Array 100 & no for
https://stackoverflow.com/questions/22826953/how-to-create-array-of-100-with-integers-from-1-1000
https://jsperf.com/fast-array-foreach
Uint8Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
https://en.wikipedia.org/wiki/Bit_array
https://stackoverflow.com/questions/29523206/how-do-i-write-an-8-bit-array-to-a-16-bit-array-of-1-2-size
https://stackoverflow.com/questions/33793246/c-converting-8-bit-values-into-16-bit-values
Typed Arrays
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
new_arr = new Uint8Array(100); // new in ES2017
// Uint8Array(100) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
old_arr = new Array(100);
// (100) [empty × 100]
js no for
& create Array 100
const arr = new Uint8Array(100).map((item, i) => i);
// Uint8Array(100) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
bitwise-operators
不用加减乘除运算符, 求整数的7倍
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-15
*
* @description bitwise-operators
* @description 不用加减乘除运算符, 求整数的7倍
* @description 7 times, without multiplication
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/11355923.html
*
*/
let log = console.log;
// 7 times, without multiplication
const autoSeventTimes = (num = 0, times = 7) => {
num = Math.abs(num);
let x = Math.floor(times / 2);
return (num << x) - num;
};
let xyz = autoSeventTimes(3);
// 21
console.log(`xyz =`, xyz);
refs
https://stackoverflow.com/questions/3746725/how-to-create-an-array-containing-1-n
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/8982974.html
未经授权禁止转载,违者必究!