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

Return '@ null' if args is null ASAP #767

Merged
merged 4 commits into from
Dec 8, 2020

Conversation

matamatanot
Copy link
Contributor

@matamatanot matamatanot commented Nov 12, 2020

If args[i] is null, always return @null.

Now, check typeof args[i] === 'string' and run String(args[i]). This is unnecessary.
This fix improve readability and performance.

@matamatanot
Copy link
Contributor Author

matamatanot commented Nov 12, 2020

I have committed into vercel:master. Was vercel:next more appropriate?
I'm skipping the error on ts-ignore using --no-verify. It seems to have already been fixed in vercel:next.

/swr/src/use-swr.ts
27:1 error Do not use "// @ts-ignore" comments because they suppress compilation errors

@shuding
Copy link
Member

shuding commented Nov 12, 2020

@matamatanot yeah if it’s not a breaking change please target master. next is for 1.0 👍

@matamatanot
Copy link
Contributor Author

There is an error. The cause is #754.

@shuding
Copy link
Member

shuding commented Nov 13, 2020

So this changes the hook behavior:

Before:

useSWR([1, 2, null, 3, 4]) // => key will be `arg@1@2@null@3@4`

Now:

useSWR([1, 2, null, 3, 4]) // => key will be `arg@1@2@null`

I think that feels inconsistent and may also cause key conflicts. But it is indeed a good question: should we skip the request if one of the args is null...?

@matamatanot
Copy link
Contributor Author

@shuding
I mistook for for forEach. Is it from a performance perspective that used for?

@matamatanot
Copy link
Contributor Author

This is an example of using reduce. It's not clear if it improved readability and performance. Instead, it may be worse.

function hash2(args: any[]): string {
  if (!args.length) return ''
  return args.reduce((acc: string, current: any) => {
    if (current === null) {
      return (acc += '@null')
    } else if (typeof current !== 'object' && typeof current !== 'function') {
      if (typeof current === 'string') {
        return (acc += '@' + '"' + current + '"')
      } else {
        return (acc += '@' + String(current))
      }
    } else {
      if (!table.has(current)) {
        table.set(current, counter++)
        return (acc += '@' + (counter - 1))
      } else {
        return (acc += '@' + table.get(current))
      }
    }
  }, 'arg')
}

@shuding
Copy link
Member

shuding commented Nov 13, 2020

Yeah I think as a small util, it's better to use for due to performance.

@matamatanot
Copy link
Contributor Author

Using the continue statement. If you don't find it useful, you can close this.

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

Thanks!

@shuding shuding merged commit fddc048 into vercel:master Dec 8, 2020
@matamatanot matamatanot changed the title Return '@null' if args is null ASAP Return '@ null' if args is null ASAP Dec 9, 2020
shuding added a commit that referenced this pull request Dec 16, 2020
* 0.3.8

* replace rIC with rAF (#744)

* Fix race condition when calling mutate synchronously (#735)

* fix race condition when calling mutate synchronously

* fix test

* add comment

* fix code reviews

* refactor: support SSR in Deno (#754)

* refactor: support SSR in Deno

* refactor: improve Deno determining

* Add @ts-ignore

Co-authored-by: Shu Ding <[email protected]>

Co-authored-by: Shu Ding <[email protected]>

* fix eslint error (#768)

* Fix `mutateCallback` types (#745)

* Fix `mutateCallback` types

* WIP

* Add CodeSandbox CI (#769)

* add CodeSandbox CI

* add new line

* fix install cmd

Co-authored-by: Paco <[email protected]>

* dispatch's payload type is actionType and run lint (#772)

* chore: payload is actionType

* chore: move a ts-ignore comment

* Fix suspense (#777)

* fix #494

* add comment

* rename to initialMountedRef

* 0.3.9

* fix: mark isValidating as false when key is falsy (#757)

* fix: tear down when key turns to empty

* use false for empty key

* Fix README.md typo (#783)

'/api/data' => '/api/user' in "Multiple Arguments"

* fix: do mount check in config callback (#787)

* Update api-hooks example README.md (#790)

Updated the Vercel deploy link to the correct directory

* Return '@null' if args is null ASAP (#767)

* chore: return 'null' if arg[i] is null ASAP

* chore: update comment

* chore: use continue

* Bump ini from 1.3.5 to 1.3.8 (#806)

Bumps [ini](https://github.com/isaacs/ini) from 1.3.5 to 1.3.8.
- [Release notes](https://github.com/isaacs/ini/releases)
- [Commits](npm/ini@v1.3.5...v1.3.8)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* update test

Co-authored-by: Shu Ding <[email protected]>
Co-authored-by: X <[email protected]>
Co-authored-by: Umidbek Karimov <[email protected]>
Co-authored-by: Paco <[email protected]>
Co-authored-by: matamatanot <[email protected]>
Co-authored-by: Jiachi Liu <[email protected]>
Co-authored-by: sAy <[email protected]>
Co-authored-by: William Crutchfield <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

2 participants