Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial implementation #1

Merged
merged 1 commit into from
Oct 29, 2022
Merged

Conversation

wemeetagain
Copy link
Member

@wemeetagain wemeetagain commented Oct 28, 2022

Implementation snatched from rust stdlib

between 30 and 300x faster than isIP from is-ip

Copy link

@dapplion dapplion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM =D

@wemeetagain wemeetagain merged commit 65211d9 into master Oct 29, 2022
@wemeetagain wemeetagain deleted the cayman/init-implementaiton branch October 29, 2022 03:12
@achingbrain
Copy link
Contributor

Can this be benched against net.isIP as well? Can do a node/browser switch out in package.json for the node api if it's faster.

@wemeetagain
Copy link
Member Author

It's there in the current benchmark.
Here's the result run on my laptop:

  isIPv4
    ✔ ours.isIPv4("0.0.0.0")                                              487092.1 ops/s    2.053000 us/op   x0.969     384097 runs  0.909 s
    ✔ ours.isIPv4("192.168.0.1")                                          480769.2 ops/s    2.080000 us/op   x0.967     300431 runs  0.707 s
    ✔ ours.isIPv4("255.255.255.255")                                      467071.5 ops/s    2.141000 us/op   x0.997     124482 runs  0.303 s
    ✔ ours.isIPv4("0.0f.0.0")                                             496524.3 ops/s    2.014000 us/op   x0.979     221674 runs  0.505 s
    ✔ ours.isIPv4("hahahahah")                                            654022.2 ops/s    1.529000 us/op   x1.006     565106 runs   1.01 s
    ✔ ours.isIPv4("999.0.0.0")                                            877193.0 ops/s    1.140000 us/op   x0.605     290316 runs  0.404 s
    ✔ ours.isIPv4("0000.0000.0000.0000")                                   2364066 ops/s    423.0000 ns/op   x1.014     448579 runs  0.303 s
    ✔ net.isIPv4("0.0.0.0")                                                3745318 ops/s    267.0000 ns/op   x0.921     988822 runs  0.505 s
    ✔ net.isIPv4("192.168.0.1")                                            3703704 ops/s    270.0000 ns/op   x0.928    1182834 runs  0.606 s
    ✔ net.isIPv4("255.255.255.255")                                        3484321 ops/s    287.0000 ns/op   x0.947     572983 runs  0.303 s
    ✔ net.isIPv4("0.0f.0.0")                                               3952569 ops/s    253.0000 ns/op   x0.927    1023410 runs  0.505 s
    ✔ net.isIPv4("hahahahah")                                              4132231 ops/s    242.0000 ns/op   x0.931    1252491 runs  0.606 s
    ✔ net.isIPv4("999.0.0.0")                                              4098361 ops/s    244.0000 ns/op   x0.928     835074 runs  0.404 s
    ✔ net.isIPv4("0000.0000.0000.0000")                                    4115226 ops/s    243.0000 ns/op   x0.927     841438 runs  0.404 s
    ✔ isIpLib.isIPv4("0.0.0.0")                                           1621.431 ops/s    616.7390 us/op   x0.989       1307 runs   1.31 s
    ✔ isIpLib.isIPv4("192.168.0.1")                                       1725.140 ops/s    579.6630 us/op   x0.949       2085 runs   1.71 s
    ✔ isIpLib.isIPv4("255.255.255.255")                                   1686.813 ops/s    592.8340 us/op   x0.999       1533 runs   1.41 s
    ✔ isIpLib.isIPv4("0.0f.0.0")                                          1787.796 ops/s    559.3480 us/op   x0.909        542 runs  0.806 s
    ✔ isIpLib.isIPv4("hahahahah")                                         1645.551 ops/s    607.6990 us/op   x0.983       2498 runs   2.02 s
    ✔ isIpLib.isIPv4("999.0.0.0")                                         1624.764 ops/s    615.4740 us/op   x0.995       1642 runs   1.51 s
    ✔ isIpLib.isIPv4("0000.0000.0000.0000")                               1595.337 ops/s    626.8270 us/op   x1.039       1769 runs   1.61 s

  isIPv6
    ✔ ours.isIPv6("1:2:3:4:5:6:7:8")                                      49438.87 ops/s    20.22700 us/op   x0.999      47206 runs   1.01 s
    ✔ ours.isIPv6("::1")                                                  111259.5 ops/s    8.988000 us/op   x1.088      30636 runs  0.303 s
    ✔ ours.isIPv6("0000:0000:0000:0000:0000:0000:0000:0000")              49880.29 ops/s    20.04800 us/op   x0.945      33623 runs  0.707 s
    ✔ ours.isIPv6("0000:0000:0000:0000:0000:ffff:192.168.100.228")        66800.27 ops/s    14.97000 us/op   x0.952      51425 runs  0.808 s
    ✔ ours.isIPv6("2001:0dc5:72a3:0000:0000:802e:3370:73E4")              58058.52 ops/s    17.22400 us/op   x0.955      27803 runs  0.505 s
    ✔ ours.isIPv6("hahahahahahaha")                                       241138.2 ops/s    4.147000 us/op        -     159521 runs  0.707 s
    ✔ ours.isIPv6("0000.0000:0000.0000")                                  221582.1 ops/s    4.513000 us/op        -     189617 runs  0.909 s
    ✔ net.isIPv6("1:2:3:4:5:6:7:8")                                        2659574 ops/s    376.0000 ns/op   x0.974    1457790 runs  0.909 s
    ✔ net.isIPv6("::1")                                                    2659574 ops/s    376.0000 ns/op   x0.913     972710 runs  0.606 s
    ✔ net.isIPv6("0000:0000:0000:0000:0000:0000:0000:0000")                2610966 ops/s    383.0000 ns/op   x0.857    1283693 runs  0.809 s
    ✔ net.isIPv6("0000:0000:0000:0000:0000:ffff:192.168.100.228")          1930502 ops/s    518.0000 ns/op   x0.893     921839 runs  0.707 s
    ✔ net.isIPv6("2001:0dc5:72a3:0000:0000:802e:3370:73E4")                2590674 ops/s    386.0000 ns/op   x0.879     957315 runs  0.606 s
    ✔ net.isIPv6("hahahahahahaha")                                         3076923 ops/s    325.0000 ns/op        -     704505 runs  0.404 s
    ✔ net.isIPv6("0000.0000:0000.0000")                                    2475248 ops/s    404.0000 ns/op        -     761644 runs  0.505 s
    ✔ isIpLib.isIPv6("1:2:3:4:5:6:7:8")                                   1598.951 ops/s    625.4100 us/op   x0.925       1941 runs   1.72 s
    ✔ isIpLib.isIPv6("::1")                                               1567.361 ops/s    638.0150 us/op   x0.958       1898 runs   1.72 s
    ✔ isIpLib.isIPv6("0000:0000:0000:0000:0000:0000:0000:0000")           1642.746 ops/s    608.7370 us/op   x0.901       1494 runs   1.41 s
    ✔ isIpLib.isIPv6("0000:0000:0000:0000:0000:ffff:192.168.100.228")     1653.641 ops/s    604.7260 us/op   x0.882       3008 runs   2.33 s
    ✔ isIpLib.isIPv6("2001:0dc5:72a3:0000:0000:802e:3370:73E4")           1566.382 ops/s    638.4140 us/op   x0.972       1107 runs   1.21 s
    ✔ isIpLib.isIPv6("hahahahahahaha")                                    1613.314 ops/s    619.8420 us/op        -       1951 runs   1.71 s
    ✔ isIpLib.isIPv6("0000.0000:0000.0000")                               1686.884 ops/s    592.8090 us/op        -       2557 runs   2.02 s

  isIP
    ✔ ours.isIP("0.0.0.0")                                                464900.0 ops/s    2.151000 us/op        -     420534 runs   1.01 s
    ✔ ours.isIP("192.168.0.1")                                            457875.5 ops/s    2.184000 us/op        -     165566 runs  0.404 s
    ✔ ours.isIP("255.255.255.255")                                        450045.0 ops/s    2.222000 us/op        -     285827 runs  0.707 s
    ✔ ours.isIP("0.0f.0.0")                                               140213.1 ops/s    7.132000 us/op        -     162464 runs   1.21 s
    ✔ ours.isIP("hahahahah")                                              172592.3 ops/s    5.794000 us/op        -      48952 runs  0.303 s
    ✔ ours.isIP("999.0.0.0")                                              180570.6 ops/s    5.538000 us/op        -      68696 runs  0.404 s
    ✔ ours.isIP("0000.0000.0000.0000")                                    179372.2 ops/s    5.575000 us/op        -      68001 runs  0.404 s
    ✔ ours.isIP("1:2:3:4:5:6:7:8")                                        50246.21 ops/s    19.90200 us/op        -      34006 runs  0.707 s
    ✔ ours.isIP("::1")                                                    106849.0 ops/s    9.359000 us/op        -      40274 runs  0.404 s
    ✔ ours.isIP("0000:0000:0000:0000:0000:0000:0000:0000")                54522.65 ops/s    18.34100 us/op        -      26091 runs  0.505 s
    ✔ ours.isIP("0000:0000:0000:0000:0000:ffff:192.168.100.228")          59417.71 ops/s    16.83000 us/op        -      22653 runs  0.404 s
    ✔ ours.isIP("2001:0dc5:72a3:0000:0000:802e:3370:73E4")                50097.69 ops/s    19.96100 us/op        -      78786 runs   1.62 s
    ✔ ours.isIP("hahahahahahaha")                                         180603.2 ops/s    5.537000 us/op        -     138441 runs  0.808 s
    ✔ ours.isIP("0000.0000:0000.0000")                                    184297.8 ops/s    5.426000 us/op        -     104967 runs  0.606 s
    ✔ net.isIP("0.0.0.0")                                                  3546099 ops/s    282.0000 ns/op        -    1176836 runs  0.606 s
    ✔ net.isIP("192.168.0.1")                                              3484321 ops/s    287.0000 ns/op        -     580345 runs  0.303 s
    ✔ net.isIP("255.255.255.255")                                          3344482 ops/s    299.0000 ns/op        -    1148451 runs  0.606 s
    ✔ net.isIP("0.0f.0.0")                                                 2433090 ops/s    411.0000 ns/op        -    2855786 runs   1.92 s
    ✔ net.isIP("hahahahah")                                                2865330 ops/s    349.0000 ns/op        -    1520122 runs  0.909 s
    ✔ net.isIP("999.0.0.0")                                                2347418 ops/s    426.0000 ns/op        -     591465 runs  0.404 s
    ✔ net.isIP("0000.0000.0000.0000")                                      2293578 ops/s    436.0000 ns/op        -     874343 runs  0.606 s
    ✔ net.isIP("1:2:3:4:5:6:7:8")                                          2450980 ops/s    408.0000 ns/op        -     767750 runs  0.505 s
    ✔ net.isIP("::1")                                                      2469136 ops/s    405.0000 ns/op        -     770679 runs  0.505 s
    ✔ net.isIP("0000:0000:0000:0000:0000:0000:0000:0000")                  2439024 ops/s    410.0000 ns/op        -     610123 runs  0.404 s
    ✔ net.isIP("0000:0000:0000:0000:0000:ffff:192.168.100.228")            1828154 ops/s    547.0000 ns/op        -     503245 runs  0.404 s
    ✔ net.isIP("2001:0dc5:72a3:0000:0000:802e:3370:73E4")                  2336449 ops/s    428.0000 ns/op        -    1489379 runs   1.01 s
    ✔ net.isIP("hahahahahahaha")                                           2923977 ops/s    342.0000 ns/op        -     684765 runs  0.404 s
    ✔ net.isIP("0000.0000:0000.0000")                                      2309469 ops/s    433.0000 ns/op        -     590156 runs  0.404 s
    ✔ isIpLib.isIP("0.0.0.0")                                             1610.942 ops/s    620.7550 us/op        -       2609 runs   2.13 s
    ✔ isIpLib.isIP("192.168.0.1")                                         1681.178 ops/s    594.8210 us/op        -       1864 runs   1.61 s
    ✔ isIpLib.isIP("255.255.255.255")                                     1694.841 ops/s    590.0260 us/op        -        342 runs  0.704 s
    ✔ isIpLib.isIP("0.0f.0.0")                                            1596.985 ops/s    626.1800 us/op        -       2258 runs   1.92 s
    ✔ isIpLib.isIP("hahahahah")                                           1598.777 ops/s    625.4780 us/op        -       2590 runs   2.13 s
    ✔ isIpLib.isIP("999.0.0.0")                                           1382.596 ops/s    723.2770 us/op        -       1403 runs   1.52 s
    ✔ isIpLib.isIP("0000.0000.0000.0000")                                 1528.769 ops/s    654.1210 us/op        -       4472 runs   3.44 s
    ✔ isIpLib.isIP("1:2:3:4:5:6:7:8")                                     1600.853 ops/s    624.6670 us/op        -       2743 runs   2.22 s
    ✔ isIpLib.isIP("::1")                                                 1514.789 ops/s    660.1580 us/op        -       1069 runs   1.21 s
    ✔ isIpLib.isIP("0000:0000:0000:0000:0000:0000:0000:0000")             1571.934 ops/s    636.1590 us/op        -       1901 runs   1.71 s
    ✔ isIpLib.isIP("0000:0000:0000:0000:0000:ffff:192.168.100.228")       1547.961 ops/s    646.0110 us/op        -       1885 runs   1.72 s
    ✔ isIpLib.isIP("2001:0dc5:72a3:0000:0000:802e:3370:73E4")             1577.449 ops/s    633.9350 us/op        -       2754 runs   2.25 s
    ✔ isIpLib.isIP("hahahahahahaha")                                      1618.807 ops/s    617.7390 us/op        -       1807 runs   1.62 s
    ✔ isIpLib.isIP("0000.0000:0000.0000")                                 1456.352 ops/s    686.6470 us/op        -       2062 runs   1.92 s

@wemeetagain
Copy link
Member Author

Can do a node/browser switch out in package.json for the node api if it's faster.

Yea good idea. It's roughly 6x faster for isIPv4 and 60x faster for isIPv6

achingbrain pushed a commit to multiformats/js-multiaddr that referenced this pull request Nov 2, 2022
See ChainSafe/is-ip#1 (comment)
New version of `@chainsafe/is-ip` provides a node-specific export and faster base implementation.
This library unaffected by the breaking change
github-actions bot pushed a commit to multiformats/js-multiaddr that referenced this pull request Nov 2, 2022
## [11.0.7](v11.0.6...v11.0.7) (2022-11-02)

### Dependencies

* update @chainsafe/is-ip ([#281](#281)) ([fc6b600](fc6b600)), closes [/github.com/ChainSafe/is-ip/pull/1#issuecomment-1295761316](https://github.com/multiformats//github.com/ChainSafe/is-ip/pull/1/issues/issuecomment-1295761316)
github-actions bot pushed a commit to libp2p/js-libp2p-utils that referenced this pull request Dec 7, 2022
## [3.0.3](v3.0.2...v3.0.3) (2022-12-07)

### Bug Fixes

* update project readme ([#66](#66)) ([7e977a2](7e977a2))

### Dependencies

* bump private-ip from 2.3.4 to 3.0.0 ([#63](#63)) ([956f404](956f404)), closes [ChainSafe/is-ip#1](ChainSafe/is-ip#1) [#19](#19) [#21](#21)
* **dev:** bump it-all from 1.0.6 to 2.0.0 ([#62](#62)) ([99cca25](99cca25)), closes [#28](#28) [#28](#28) [#27](#27) [#24](#24)
* **dev:** bump it-map from 1.0.6 to 2.0.0 ([#61](#61)) ([88b05b4](88b05b4))
* **dev:** bump uint8arrays from 3.1.1 to 4.0.2 ([#60](#60)) ([ca0b632](ca0b632)), closes [#41](#41) [#40](#40) [#28](#28) [#41](#41) [#40](#40) [#28](#28) [#41](#41) [#40](#40) [#28](#28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants