Skip to content

Commit

Permalink
something
Browse files Browse the repository at this point in the history
  • Loading branch information
yk committed May 5, 2024
1 parent e65ddae commit 526e267
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 46 deletions.
16 changes: 13 additions & 3 deletions content/posts/algorithm/z-others/JS类常见一般算法题.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ function arrToTree(arr) {
/**
* 2024.02.15 重新写了一版,应该更简单
*/
const transToTree = (arr, pid) => {
function arrToTree(arr, pid) {
if (!arr.length) return
const rootItems = arr.filter(item => item.pid === pid)
for (const el of rootItems) {
el.children = arr.filter(item => item.pid === el.id)
arrToTree(arr, el.id)
}
return rootItems
}
```

<!-- const transToTree = (arr, pid) => {
if (!arr.length) return
const rootItems = arr.filter(item => item.pid === pid)
for (let i = 0; i < rootItems.length; ++i) {
Expand All @@ -121,8 +132,7 @@ const transToTree = (arr, pid) => {
transToTree(arr, item.id)
}
return rootItems
}
```
} -->

上方是写出来了,但是呢,这个复杂度有点高,怎么优化呢?往往需要借助数据结构 Map:

Expand Down
23 changes: 10 additions & 13 deletions content/posts/base-js/常用内置原理.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ tags: [JavaScript]
### new

```js
function _new(constructor, ...args) {
const obj = Object.create(constructor.prototype)
const ret = constructor.call(obj, ...args)
function _new(ctor, ...args) {
const obj = Object.create(ctor.prototype)
const ret = ctor.call(obj, ...args)
const isFunc = typeof ret === 'function'
const isObj = typeof ret === 'object' && ret !== null
return isFunc || isObj ? ret : obj
Expand Down Expand Up @@ -120,23 +120,20 @@ function deepClone(target, wm = new WeakMap()) {
### Promise.all & Promise.race
```js
function promiseAll(promises) {
const promiseAll = promises => {
let res = []
let count = 0
const res = []
return new Promise((resolve, reject) => {
promises.forEach((p, index) => {
Promise.resolve(p).then(r => {
return new Promise(resolve => {
promises.forEach((p, i) => {
p().then(r => {
res[i] = r
count++
res[index] = r
if (count === promises.length) resolve(res)
})
})
})
}

promiseAll([mockReq(2000), mockReq(1000), mockReq(3000)]).then(res => {
console.log(res)
})
promiseAll(reqs).then(res => console.log('🔥 ---', res))

/* ---------- race ---------- */
function promiseRace(promises) {
Expand Down
2 changes: 1 addition & 1 deletion public/index.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/tags/javascript/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ js
<name>EricYuan</name>
</author><guid>http://localhost:1313/%E5%B8%B8%E7%94%A8%E5%86%85%E7%BD%AE%E5%8E%9F%E7%90%86/</guid>
<description><![CDATA[new js
function _new(constructor, ...args) { const obj = Object.create(constructor.prototype) const ret = constructor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
function _new(ctor, ...args) { const obj = Object.create(ctor.prototype) const ret = ctor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
/** * ins - instance * ctor - constructor */ function _instanceof(ins, ctor) { let __proto__ = ins.__proto__ let prototype = ctor.prototype while (true) { if (__proto__ === null) return false if (__proto__ === prototype) return true __proto__ = __proto__.]]></description>
</item><item>
<title>位运算</title>
Expand Down
27 changes: 12 additions & 15 deletions public/常用内置原理/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noodp" /><title>常用内置 API 的实现 - Hello World</title><meta name="Description" content="A website documenting front-end technology and everyday life"><meta property="og:title" content="常用内置 API 的实现" />
<meta property="og:description" content="new js
function _new(constructor, ...args) { const obj = Object.create(constructor.prototype) const ret = constructor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
function _new(ctor, ...args) { const obj = Object.create(ctor.prototype) const ret = ctor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
/** * ins - instance * ctor - constructor */ function _instanceof(ins, ctor) { let __proto__ = ins.__proto__ let prototype = ctor.prototype while (true) { if (__proto__ === null) return false if (__proto__ === prototype) return true __proto__ = __proto__." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://localhost:1313/%E5%B8%B8%E7%94%A8%E5%86%85%E7%BD%AE%E5%8E%9F%E7%90%86/" /><meta property="og:image" content="http://localhost:1313/" /><meta property="article:section" content="posts" />
Expand All @@ -18,7 +18,7 @@
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="http://localhost:1313/" /><meta name="twitter:title" content="常用内置 API 的实现"/>
<meta name="twitter:description" content="new js
function _new(constructor, ...args) { const obj = Object.create(constructor.prototype) const ret = constructor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
function _new(ctor, ...args) { const obj = Object.create(ctor.prototype) const ret = ctor.call(obj, ...args) const isFunc = typeof ret === &#39;function&#39; const isObj = typeof ret === &#39;object&#39; &amp;&amp; ret !== null return isFunc || isObj ? ret : obj } instanceof js
/** * ins - instance * ctor - constructor */ function _instanceof(ins, ctor) { let __proto__ = ins.__proto__ let prototype = ctor.prototype while (true) { if (__proto__ === null) return false if (__proto__ === prototype) return true __proto__ = __proto__."/>
<meta name="application-name" content="前端后花园">
<meta name="apple-mobile-web-app-title" content="前端后花园">
Expand Down Expand Up @@ -320,9 +320,9 @@ <h2 class="toc-title">目录</h2>
group-[.is-closed]:!tw-max-h-0
group-[.is-wrap]:tw-text-wrap
tw-overflow-y-hidden
tw-overflow-x-auto"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">_new</span><span class="p">(</span><span class="nx">constructor</span><span class="p">,</span> <span class="p">...</span><span class="nx">args</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">obj</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">prototype</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">constructor</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">...</span><span class="nx">args</span><span class="p">)</span>
tw-overflow-x-auto"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">_new</span><span class="p">(</span><span class="nx">ctor</span><span class="p">,</span> <span class="p">...</span><span class="nx">args</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">obj</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">ctor</span><span class="p">.</span><span class="nx">prototype</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">ctor</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">...</span><span class="nx">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">isFunc</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">ret</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">isObj</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">ret</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">ret</span> <span class="o">!==</span> <span class="kc">null</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">isFunc</span> <span class="o">||</span> <span class="nx">isObj</span> <span class="o">?</span> <span class="nx">ret</span> <span class="o">:</span> <span class="nx">obj</span>
Expand Down Expand Up @@ -960,23 +960,20 @@ <h3 id="promiseall--promiserace" class="headerLink">
group-[.is-closed]:!tw-max-h-0
group-[.is-wrap]:tw-text-wrap
tw-overflow-y-hidden
tw-overflow-x-auto"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">promiseAll</span><span class="p">(</span><span class="nx">promises</span><span class="p">)</span> <span class="p">{</span>
tw-overflow-x-auto"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">promiseAll</span> <span class="o">=</span> <span class="nx">promises</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kd">let</span> <span class="nx">res</span> <span class="o">=</span> <span class="p">[]</span>
</span></span><span class="line"><span class="cl"> <span class="kd">let</span> <span class="nx">count</span> <span class="o">=</span> <span class="mi">0</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">res</span> <span class="o">=</span> <span class="p">[]</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">((</span><span class="nx">resolve</span><span class="p">,</span> <span class="nx">reject</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">promises</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">p</span><span class="p">,</span> <span class="nx">index</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nb">Promise</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">p</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="nx">r</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">(</span><span class="nx">resolve</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">promises</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">p</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">p</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="nx">r</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">res</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">r</span>
</span></span><span class="line"><span class="cl"> <span class="nx">count</span><span class="o">++</span>
</span></span><span class="line"><span class="cl"> <span class="nx">res</span><span class="p">[</span><span class="nx">index</span><span class="p">]</span> <span class="o">=</span> <span class="nx">r</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">count</span> <span class="o">===</span> <span class="nx">promises</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="nx">resolve</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">})</span>
</span></span><span class="line"><span class="cl"> <span class="p">})</span>
</span></span><span class="line"><span class="cl"> <span class="p">})</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nx">promiseAll</span><span class="p">([</span><span class="nx">mockReq</span><span class="p">(</span><span class="mi">2000</span><span class="p">),</span> <span class="nx">mockReq</span><span class="p">(</span><span class="mi">1000</span><span class="p">),</span> <span class="nx">mockReq</span><span class="p">(</span><span class="mi">3000</span><span class="p">)]).</span><span class="nx">then</span><span class="p">(</span><span class="nx">res</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span><span class="line"><span class="cl"><span class="nx">promiseAll</span><span class="p">(</span><span class="nx">reqs</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="nx">res</span> <span class="p">=&gt;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;🔥 ---&#39;</span><span class="p">,</span> <span class="nx">res</span><span class="p">))</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="cm">/* ---------- race ---------- */</span>
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">promiseRace</span><span class="p">(</span><span class="nx">promises</span><span class="p">)</span> <span class="p">{</span>
Expand Down
23 changes: 10 additions & 13 deletions public/常用内置原理/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
### new

```js
function _new(constructor, ...args) {
const obj = Object.create(constructor.prototype)
const ret = constructor.call(obj, ...args)
function _new(ctor, ...args) {
const obj = Object.create(ctor.prototype)
const ret = ctor.call(obj, ...args)
const isFunc = typeof ret === 'function'
const isObj = typeof ret === 'object' && ret !== null
return isFunc || isObj ? ret : obj
Expand Down Expand Up @@ -117,23 +117,20 @@ function deepClone(target, wm = new WeakMap()) {
### Promise.all & Promise.race
```js
function promiseAll(promises) {
const promiseAll = promises => {
let res = []
let count = 0
const res = []
return new Promise((resolve, reject) => {
promises.forEach((p, index) => {
Promise.resolve(p).then(r => {
return new Promise(resolve => {
promises.forEach((p, i) => {
p().then(r => {
res[i] = r
count++
res[index] = r
if (count === promises.length) resolve(res)
})
})
})
}

promiseAll([mockReq(2000), mockReq(1000), mockReq(3000)]).then(res => {
console.log(res)
})
promiseAll(reqs).then(res => console.log('🔥 ---', res))

/* ---------- race ---------- */
function promiseRace(promises) {
Expand Down

0 comments on commit 526e267

Please sign in to comment.