liangerwen

手写题盘点

字数统计:723|阅读时长:6分钟|发表于:2025-02-25

广度遍历深克隆

JAVASCRIPT
  1. 1const getEmpty = (o) => {
  2. 2 if (Array.isArray(o)) return [];
  3. 3 if (o !== null && typeof o === "object") return {};
  4. 4 return o;
  5. 5};
  6. 6
  7. 7const cloneBfs = (a) => {
  8. 8 const root = getEmpty(a),
  9. 9 queue = [{ origin: a, copy: root }],
  10. 10 map = new Map();
  11. 11
  12. 12 while (queue.length) {
  13. 13 const { origin, copy } = queue.shift();
  14. 14 for (let i in origin) {
  15. 15 const empty = getEmpty(origin[i]);

限制 promise 并发数量

JAVASCRIPT
  1. 1const pLimit = (limit) => {
  2. 2 let count = 0;
  3. 3 const task = [];
  4. 4 return async (fn) => {
  5. 5 if (count >= limit) {
  6. 6 await new Promise((r) => task.push(r));
  7. 7 }
  8. 8 count++;
  9. 9 try {
  10. 10 const ret = await fn();
  11. 11 return ret;
  12. 12 } finally {
  13. 13 count--;
  14. 14 if (task.length) {
  15. 15 task.shift()();

liangerwen

liangerwen

这瓜娃子懒得很,什么都没有留哈!