xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js no for & 100 Array

js no for 100 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"]

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');


image

for is better than forEach

image

map

image


Array 100 & no for

https://www.quora.com/Given-an-array-with-100-elements-numbers-from-0-to-99-if-I-took-a-random-element-out-then-how-would-you-find-that-which-one-I-took-out-How-would-you-solve-this-if-1-The-array-is-sorted-or-2-The-array-is-not-sorted

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

image

image

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]

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]

image


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);


https://hiluluke.cn/



Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/8982974.html