我们一起来做一道 JS 题目:在控制台打印 React 仓库的 Fork 列表

2017-07-11 13:34:36 +08:00
 bw2

要求


获取当前页面已 Fork 的成员列表,存储在数组。代码短,耗时少者更佳。返回数组长度应该是 1000。

地址

https://github.com/facebook/react/network/members

示例代码


// 时间和函数长度统计

function timeAndLengthCount(callback){
  const timeStart = new Date().getTime()
  callback()
  const timeEnd = new Date().getTime()
  console.log('Time: ' + (timeEnd-timeStart) + 'ms')
  console.log('Len: '+ callback.toString().length)
}

// 执行输出 Fork 成员列表的函数
// Time: 7ms
// Len: 223

timeAndLengthCount(()=>{
  const forkedMenber = document.querySelectorAll('.repo > a:nth-child(odd)');
  const forkedMenberList = [];
  [].forEach.call(forkedMenber, z => forkedMenberList.push(z.innerHTML));
  console.log(forkedMenberList);
})

接下来就是开始你的表演了

4361 次点击
所在节点    程序员
47 条回复
nino
2017-07-11 13:56:08 +08:00
[...document.querySelectorAll('#network .repo > a:first-of-type')].map(el => el.textContent)
xiaojie668329
2017-07-11 13:59:37 +08:00
把 querySelectorAll 换成 getElementsBy...会变快。
zjsxwc
2017-07-11 14:00:43 +08:00
剩下的就是 css 选择器的花式写法了
bw2
2017-07-11 14:03:51 +08:00
@nino 厉害了,把我的代码压缩成了一行,就扶你
bw2
2017-07-11 14:04:54 +08:00
@xiaojie668329 确实,然后使用正则?
bw2
2017-07-11 14:05:25 +08:00
@zjsxwc 我觉得还有别的玩法
bw2
2017-07-11 14:08:45 +08:00
最新记录是 @nino
Time: 2ms
Len: 96
Wangxf
2017-07-11 14:09:38 +08:00
[...document.querySelectorAll('.repo')].slice(1).map((item)=>item.children[2].innerHTML)
crs0910
2017-07-11 14:14:11 +08:00
document.querySelector('#network').textContent.replace(/\s/g, '').split('/react')
bw2
2017-07-11 14:17:34 +08:00
@Wangxf

Time: 4ms
Len: 92
bw2
2017-07-11 14:18:56 +08:00
@crs0910
Time: 2ms
Len: 85

但是,返回数组是 1001,数组的第一个'facebook',不在需求范围内
hueo
2017-07-11 14:20:38 +08:00
要考虑性能的话正则是最快的。

不过昨天的代码是这样的:

Array.from(document.querySelectorAll('#network .repo > img + a'), e => e.href)
crs0910
2017-07-11 14:21:27 +08:00
加个 shift 不就得了,我测是 1ms
crs0910
2017-07-11 14:28:10 +08:00
@xiaojie668329 貌似 querySelector\querySelectorAll 都比 getElementById\getElementsByTag 要快,我是最新的 Chrome.
https://jsperf.com/getelementsbytagname-a-0-vs-queryselector-a/4
bw2
2017-07-11 14:32:33 +08:00
@crs0910 加 shift 之后返回的是'facebook',你试试,需求是打印一个数组,长度 1000
bw2
2017-07-11 14:33:33 +08:00
@hueo 就是昨天看到你的,才突发奇想的
Wangxf
2017-07-11 14:37:32 +08:00
[...document.getElementById('network').getElementsByTagName('a')].filter((item,index)=>index%2===0).map((item)=>item.innerHTML).slice(1);
crs0910
2017-07-11 14:40:30 +08:00
@bw2
document.getElementById('network').textContent.replace(/\s/g, '').split('/react').slice(1)) document.getElementById('network').textContent.replace(/\s/g, '').split('/react').splice(1))
Wangxf
2017-07-11 14:44:57 +08:00
最新版 1.5s
[...document.getElementById('network').querySelectorAll('a:nth-child(odd)')].slice(1).map((item)=>item.innerHTML)
crs0910
2017-07-11 15:01:22 +08:00
去掉一个 replace 的版本

document.querySelector('#network').textContent.split(/\s*\/\s*react\s*/g).splice(1)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/374493

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX